sql server默認不區分大小寫查詢,但是有的時候部分查詢語句卻需要區分大小寫查詢,這個時候就需要進行一些特殊處理。區分大小寫主要分兩種方法。
轉二進制判斷
select * from table where cast(name as varbinary)=cast('LiYuanBa' as varbinary) --短字符串 select * from table where cast(name as varbinary)=cast('LiYuanBaABCEDEF……' as varbinary(500)) --長字符串
注意
varbinary默認長度為30,如果長度不夠不保留超出的部分,最終導致判斷錯誤!
通過collate Chinese_PRC_CS_AS
select * from table where name collate Chinese_PRC_CS_AS='LiYuanBa' --精確 select * from table where name collate Chinese_PRC_CS_AS like 'LiYuanBa%' --模糊
優點
不需要考慮字符串長度問題,建議使用。