sql server区分大小写查询

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/y1535623813/article/details/88600792

sql server默认不区分大小写查询,但是有的时候部分查询语句却需要区分大小写查询,这个时候就需要进行一些特殊处理。区分大小写主要分两种方法。

转二进制判断

1

2

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

1

2

select from table where name collate Chinese_PRC_CS_AS='LiYuanBa'        --精确

select from table where name collate Chinese_PRC_CS_AS like 'LiYuanBa%'  --模糊

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

group by 区分大小写:
select (filed collate Chinese_PRC_CS_AI) as filed from table
group by (filed collate Chinese_PRC_CS_AI)  

猜你喜欢

转载自blog.csdn.net/y1535623813/article/details/88600792