字符类型的Collation

在SQL Server中,字符类型主要是指单字节字符(char,varchar)和 unicode字符(nchar,nvarchar)。字符类型的排序和比较是由Collation决定的。对于单字节字符,Collation决定字符的Code Page和字符数据的呈现。

Collations that are used with character data types such as char and varchar dictate the code page and corresponding characters that can be represented for that data type.

一,与字符数据关联的三个关属性:Locale,Code Page 和 Sort Order。

1,Locale ,关联区域或语言文化,不同的语言有不同的字符集。

A locale is a set of information that is associated with a location or a culture. This can include the name and identifier of the spoken language, the script that is used to write the language, and cultural conventions. Collations can be associated with one or more locales.

2,Code Page,关联到OS支持的字符数据集

A code page is an ordered set of characters of a given script in which a numeric index, or code point value, is associated with each character. A Windows code page is typically referred to as a character set or charset. Code pages are used to provide support for the character sets and keyboard layouts that are used by different Windows system locales.

Code Page是由OS设置决定的,通过设置Windows OS的区域来设置OS使用的Code Page。

The code pages that a client uses are determined by the operating system settings. To set client code pages on the Windows operating system, use Regional Settings in Control Panel.

3,Sort Order,字符数据的排序

Sort order specifies how data values are sorted. This affects the results of data comparison. Data is sorted by using collations, and it can be optimized by using indexes.

二,Collation

在SQL Server中,Collation 由两部分组成:Sorting Rule(对于non-unicode字符,还包括Code Page) 和 Comparison Style,用于对字符的呈现,排序和比较。对于non-unicode字符,必须指定一个Code Page。在不同Code Page之间进行传递字符数据,必须将Source Code Page转换成 Destination Code Page。对于Unicode 字符,不需要code page,在不同机器之间传递字符数据集,不需要进行code page的转换,这能提高数据处理的性能。

comparison style主要是case sensitivity, accent sensitivity, Kana-sensitivity, width sensitivity。

A collation specifies the bit patterns that represent each character in a data set. Collations also determine the rules that sort and compare data. SQL Server supports storing objects that have different collations in a single database. For non-Unicode columns, the collation setting specifies the code page for the data and which characters can be represented. Data that is moved between non-Unicode columns must be converted from the source code page to the destination code page.

三,Unicode Support

non-unicode使用一个字节表示一个字符,由于一个字节能够表示的字符是有限的,不同的语言或区域,使用Code Page来区分不同的字符数据集。每一个字符集都拥有一个Code Page。对于使用non-unicode的computer,每台机器只能设置一个code page。在Code Page不同的机器上进行字符数据传递时,必须进行Code Page的转换。而Unicode 编码使用两个字节(2Byte)表示一个字符,能够表示世界上所有的字符数据集,因此不再需要Code Page。

Unicode is a standard for mapping code points to characters. Because it is designed to cover all the characters of all the languages of the world, there is no need for different code pages to handle different sets of characters. If you store character data that reflects multiple languages, always use Unicode data types (nchar, nvarchar, and ntext) instead of the non-Unicode data types (char, varchar, and text).

Significant limitations are associated with non-Unicode data types. This is because a non-Unicode computer will be limited to use of a single code page. You might experience performance gain by using Unicode because fewer code-page conversions are required. Unicode collations must be selected individually at the database, column or expression level because they are not supported at the server level.

The code pages that a client uses are determined by the operating system settings. To set client code pages on the Windows operating system, use Regional Settings in Control Panel.

四,查看SQL Server支持的Collation 及其关联的Code Page

select * ,collationproperty(name, ‘codepage‘) as CodePage
from sys.fn_helpcollations()

常见的三个Code Page:

936:简体中文

1252:Latin1,兼容ASCII,单字节编码

65001:UTF-8 Unicode

引用《Unicode 和 UTF-8 有何区别?》:

计算机内只能保存101010等二进制数据,那么页面上显示的字符是如何显示出来的呢?

一:字符集(Charset)

charset = char + set,char 是字符,set是集合,charset就是字符的集合。

字符集就是是这个编码方式涵盖了哪些字符,每个字符都有一个数字序号。

二:编码方式(Encoding)

编码方式就是一个字符要怎样编码成二进制字节序,或者反过来怎么解析。 也即给你一个数字序号,要编码成几个字节,字节顺序如何,或者其他特殊规则。

三:字形字体(Font)

根据数字序号调用字体存储的字形,就可以在页面上显示出来了。 所以一个字符要显示出来,要显示成什么样子要看字体文件。
综上所述,Unicode 只是字符集,而没有编码方式。 UTF-8 是一种 Unicode 字符集的编码方式,其他还有 UTF-16,UTF-32 等。

而有了字符集以及编码方式,如果系统字体是没有这个字符,也是显示不出来的。

参考doc:

Collation and Unicode Support

COLLATE (Transact-SQL)

COLLATIONPROPERTY (Transact-SQL)

sys.fn_helpcollations (Transact-SQL)

时间: 2024-08-01 14:02:36

字符类型的Collation的相关文章

MySQL学习分享-->字符类型

字符类型 首先我们会讲到字符集的概念,字符集是指一些字符的集合,而字符编码是指一个字符的编码格式,因此两者的关系可以这样描述,例如:"使用Unicode编码的字符集",意思就是该字符集中的所有字符都是使用Unicode进行编码的. 接着说一下Unicode,它是一种字符编码,不是字符集,它为每种语言的每个字符设定了统一且唯一的二进制编码.在MySQL5.5及以上版本中支持ucs2.utf8.utf8mb4.utf16.utf32五种Unicode编码的字符集,而在MySQL5.5版本之

跟王老师学MySQL:MySQL数据类型之字符类型

跟王老师学MySQL:MySQL数据类型之字符类型 主讲教师:王少华   QQ群号:483773664 学习内容: 字符串类型的种类及其特点 char和varchar的异同 字符串类型是在数据库中存储字符串的数据类型. 字符串类型包括 CHAR. VARCHAR BLOB TEXT 一.char和varchar (一)定义语法 1 字符串类型 (M) 注: 字符串类型:char或varchar M:指定了该字符串最大长度 (二)二者不同之处 char类型长度是固定的,即在创建表时就指定了,其长度

数据库里面几个字符类型的区别

上图是数据库里面几个字符类型的区别,这个应该都知道.还有一点需要提及的是.varchar和nvarchar虽然是可变长的字符类型,但是,也只会在范围内变长而已,例如varchar(20)的极限就是20个字节了,如果值超出了20个字节就会被截取,并不会自动变长的.所以以后在定义的时候,比最大字符串还大一点就好了,这就意味着你要一个一个数了........ 上来就直接定义很大的一个范围的不推荐,造成的磁盘浪费很不好.....

关于未成品的问题:字符类型和其他种种

0.2版本的随机出题器,问题还有,等着下次解决.先把作业交了再说. 完全重做了,基于VC(偷懒所以使用了MFC的CString类),但是也导致了字符类型的问题. 首先是大概:出题和答案分成了两个类,主函数用来控制参数和计算分数等. 1.答案部分(一个算数计算器) 该计算器支持+-*/^五种运算符,也支持括号(包括多重) 1 double EvaluateExpression(char* MyExpression) 2 { 3 //表达式求值的算符优先算法. 4 //设OPTR和OPND分别为运算

sql:将字符类型字段转换成数字并排序

使用cast 函数可以把字符类型字段(数学形式)转换为数字 比如 1 SELECT c.unit_no, c.name,m.apply_no,m.MEETING_TYPE,m.meeting_no,m.BOOTH FROM nfrc.meeting_apply m,company c WHERE c.unit_no(+) = m.unit_no AND m.auth = 1 AND m.nfrc_meeting_no=? ORDER BY cast(m.BOOTH AS INT) ASC ";

指针与字符串--字符类型

字符类型 char(character)是一种整数,也是一种特殊的类型:字符.这是因为 ① 用单引号表示的字符字符字面量:‘a’,'1' ②‘’也是一个字符 ③printf和scanf里用%c来输入.输出字符. 1 #include<stdio.h> 2 int main() 3 { 4 char c; 5 char d; 6 c = 1; 7 d = '1'; 8 printf("c = %d,d = %d,d = %c\n",c,d,d); 9 return 0; 10

Mysql 支持的字符类型

MySQL中定义数据字段的类型对你数据库的优化是非常重要的.尤其和oracle某些不一致. 比如没有number类型,特此总结下来方便以后查询 MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准SQL数值数据类型. 这些类型包括严格数值数据类型(INTEGER.SMALLINT.DECIMAL和NUMERIC),以及近似数值数据类型(FLOAT.REAL和DOUBLE PRECISION). 关键字INT是INTEGER的同义词,关

交叉报表列头排序时遇到的oracle问题—oracle ORA-12704:字符集不匹配、varchar2转化为nvarchar2字符缺失、case when else后的字符类型要一致

在做交叉报表列头的排序时,遇到这三个问题,下面具体来说一下. 设计的数据库的表结构如图1所示: 图1 要处出来student_name_,s.grade_,s.subject_name_,这三个属性,当时我是这样写的sql语句: select  s.student_name_, s.grade_,  s.subject_name_, case  s.subject_name_ when  '语文' then 'A语文' when  '数学' then 'B数学' when  '英语' then 

为JS字符类型添加trim方法

JS字符串本身并不没有提供常用的trim方法,我们可以通过修改String原形来添加此方法,我们就可以直接调用此方法了: String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, '')} String.prototype.leftTrim = function(){return this.replace(/(^\s*)/g, '')} String.prototype.rigthTrim = functio