以MySQL為例 ,在建立新的資料表時 ,會為指定的資料夾記錄欄位指定編碼方式 ,以當前為了製作多語系的網站內容 ,通常會指定為 UTF8 的部分 ,很多書都在寫 ,指定資料表時 ,只要指定 utf8_unicode_ci 即可, 但是在下拉編碼清單裡面 可以看見除了  utf8_unicode_ci  之外的UTF8選項還有 utf8_bin 可以選擇 ,那麼 究竟 utf8_bin 和  utf8_unicode_ci 差別在哪裡呢?

SearchResult1 

SearchResult2 

其實 ,   utf8_unicode_ci 後面的 _ci 代表著 case insensitive 的意思 ,也就是說 當使用Like 進行資料比對時 ,不會區分大小寫.

若大小寫字串需有不同的比對結果時 ,則選擇 (也就是 Frank 和 frank 是不相同的) utf8_bin 即可.

以上為個人心得 ,歡迎交流意見 ,謝謝.

--- 以下為轉貼網友所分享的 Sql 文字編碼的字符集 的搜尋比對相關字串. ---

字符集的encoding、collation的问题。字符比较和collation有关。

在sql2000和7.0的查询语句中,区分大写的查询方法

--sql2000,就用下面的方法.
--就是在字段名后加 collate Chinese_PRC_CS_AS_WS


--区分大小写、全半角字符的方法

--测试数据
create table 表(fd varchar(10))
insert into 表
select aa='aa'
union all select 'Aa'
union all select 'AA' --全角A
union all select 'A,A' --全角A,半角,
union all select 'A,A' --全角A,全角,
go

--查询
--1.查大写字母
select * from 表
where fd collate Chinese_PRC_CS_AS_WS like '%A%'
--就是在字段名后加 collate Chinese_PRC_CS_AS_WS

--2.查全角
select * from 表
where fd collate Chinese_PRC_CS_AS_WS like '%A%'

--3.查半角
select * from 表
where fd collate Chinese_PRC_CS_AS_WS like '%,%'
go

--删除测试数据
drop table 表

/*--测试结果

1.查询大写字母的结果
fd
----------
Aa


2.查询全角字符的结果
fd
----------
AA
A,A
A,A


3.查询半角字符的结果
fd
----------
A,A

(所影响的行数为 1 行)
--*/


================================================================

--sql7.0,就用下面的方法.

--如果是全部比较
--下面是测试
select * from(
select fd='a'
union all select 'A'
) a
where cast(fd as varbinary(8000))=cast('A' as varbinary(8000))

/*--测试结果
fd
----
A

(所影响的行数为 1 行)
--*/

--如果是部分匹配,就用charindex:

--下面是测试
select * from(
select fd='a'
union all select 'A'
union all select 'aAaa'
union all select 'aaaa'
union all select 'cccA'
) a
where charindex(cast('A' as varbinary(8000)),cast(fd as varbinary(8000)))>0

/*--测试结果
fd
----
A
aAaa
cccA

(所影响的行数为 3 行)
--*/

arrow
arrow
    全站熱搜

    Frank 發表在 痞客邦 留言(0) 人氣()