2008年9月8日

SQL Server: case sensitive string comparison

SQL Server預設的COLLATE並沒有區分大小寫,因此以下兩句SQL查詢結果都是1。
select count(1) where 'test'='TEST'
select count(1) where 'test' like 'TEST'

比較簡單的解決辦法,就是用difference作比較或是convert轉換成varbinary,以下兩句SQL有區別大小寫,因此傳回0。
select count(1) where difference('test', 'TEST') = 0
select count(1) where convert(varbinary, 'test') = convert(varbinary, 'TEST')

沒有留言:

張貼留言

lyhcode by lyhcode
歡迎轉載,請務必註明出處!