SqlServer区分大小写查询

SqlServer区分大小写查询

 

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%'  --模糊

优点
不需要考虑字符串长度问题,建议使用。

  

posted @ 2020-06-11 10:38  徐鲜  阅读(371)  评论(0编辑  收藏  举报