SQL Server数据库查询区分大小写、全半角——排序规则的应用(转载)
SQL Server數據庫查詢區分大小寫、全半角——排序規則的應用
因為偶然的原因,需要在INNER JOIN聯表時,讓對應字段進行區分大小寫的比較。而默認情況下建立的Sql Server數據庫是不區分大小寫的,這個需求怎么實現呢?
要實現這個需求,至少有三個操作級別來實現:
1.?數據庫級別:整個數據庫中的char、varchar、text、nchar、nvarchar?和?ntext?數據都區分大小寫。(為描述方便,下文不再明確強調這些數據類型。)但這樣做有個明顯的壞處,那就是整個數據庫的這些字段的比較,都要進行嚴格匹配。比如下面的兩條Sql語句會得到完全不同的兩種結果:
SELECT * FROM [TABLE] WHERE [COLUMN] LIKE ‘%KeyWord%’ SELECT * FROM [TABLE] WHERE [COLUMN] LIKE ‘%keyword%’因為數據庫在比較時會嚴格按照大小寫來區別,這樣就會導致我們在程序開發過程中難以滿足十分復雜的查詢需求,特別突出的例子便是關鍵字搜索——我們無法預知客戶輸入內容的大小寫,我們更不能要求客戶輸入的關鍵字區分大小寫。
2.?表字段級別:只對特定的表字段進行大小寫區分。這樣做在整體上減小了大小寫限定的范圍,從全數據庫減小到當前指定的字段。但使用起來仍然有些局限性。
3.?查詢級別:只對本次查詢限定大小寫區分。這也是本文重點介紹的實現方式。這樣操作可以讓我們不對以前的數據庫做任何的修改,只對當前這次查詢區分大小寫,也不會影響程序中別處對這些字段的查詢。
好了,上文說了三個實現級別,那具體怎么實現呢?這就要用到SQL數據庫中的“排序規則”了。我們可以在數據庫的屬性上,設置排序規則,也可以在表設計的字段屬性里設置排序規則。具體要設置什么樣的排序規則,后面來說,我們先說查詢級別的排序規則。下面有一段SQL片段可以演示在查詢級別區分大小寫的排序規則應用:
use tempdb set nocount on --1-- print '建立初始數據表Customer' create table Customer(id int,uname varchar(10)) insert into Customer select 1,'Jim' union all select 2,'Simith' union all select 3,'uonun'select * from Customer--2-- print '建立初始數據表Info' create table Info(uname varchar(10),phone varchar(11)) insert into Info select 'JIM','13800000000' union all select 'Simith','13911111111' union all select 'uonun','13812345678'select * from Info--3-- print '不區分大小寫,不區分全半角' select c.id,c.uname as 'c.uname',i.uname as 'i.uname',i.phone from Customer as c inner join Info as i on c.uname = i.uname--4-- print '區分大小寫,不區分全半角' select c.id,c.uname as 'c.uname',i.uname as 'i.uname',i.phone from Customer as c inner join Info as i on c.uname = i.uname collate Chinese_PRC_CS_AS--5-- print '不區分大小寫,區分全半角' select c.id,c.uname as 'c.uname',i.uname as 'i.uname',i.phone from Customer as c inner join Info as i on c.uname = i.uname collate Chinese_PRC_CI_AI_WS--6-- print '區分大小寫,區分全半角' select c.id,c.uname as 'c.uname',i.uname as 'i.uname',i.phone from Customer as c inner join Info as i on c.uname = i.uname collate Chinese_PRC_CS_AI_WSDROP TABLE Customer DROP TABLE Info/* 建立初始數據表Customer id uname ----------- ---------- 1 Jim 2 Simith 3 uonun建立初始數據表Info uname phone ---------- ----------- JIM 13800000000 Simith 13911111111 uonun 13812345678不區分大小寫,不區分全半角 id c.uname i.uname phone ----------- ---------- ---------- ----------- 1 Jim JIM 13800000000 2 Simith Simith 13911111111 3 uonun uonun 13812345678區分大小寫,不區分全半角 id c.uname i.uname phone ----------- ---------- ---------- ----------- 2 Simith Simith 13911111111 3 uonun uonun 13812345678不區分大小寫,區分全半角 id c.uname i.uname phone ----------- ---------- ---------- ----------- 1 Jim JIM 13800000000 3 uonun uonun 13812345678區分大小寫,區分全半角 id c.uname i.uname phone ----------- ---------- ---------- ----------- 3 uonun uonun 13812345678 */通過上面的SQL語句可以看出,我們在查詢時使用COLLATE字句,指定排序規則可以影響查詢結果。通過上面的圖也可以看出,這個排序規則除可以區分大小寫之外,還可以區分重音、假名、全半角。
結束語:
?
原文地址:http://blog.udnz.com/Article/use-COLLATE-in-Transact-SQL.aspx
轉載于:https://www.cnblogs.com/RoseDavid/archive/2012/10/19/2730635.html
總結
以上是生活随笔為你收集整理的SQL Server数据库查询区分大小写、全半角——排序规则的应用(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue中render: h = h(Ap
- 下一篇: 代码练习中的bug及修改方法