在sql2005的查询语句中,区分大写的查询方法

--sql2005,就用下面的方法.
  --就是在字段名后加 collate Chinese_PRC_CS_AS_WS
  --区分大小写、全半角字符的方法
  --测试数据
create table 表(fd varchar(10))
insert into 表
select aa='aa'
union all select 'Aa'
union all select 'AA' --全角A
union all select 'A,A' --全角A,半角,
union all select 'A,A' --全角A,全角,
go

  --查询
  --1.查大写字母
select * from 表
where fd collate Chinese_PRC_CS_AS_WS like '%A%'
--就是在字段名后加 collate Chinese_PRC_CS_AS_WS

  --2.查全角
select * from 表
where fd collate Chinese_PRC_CS_AS_WS like '%A%'

  --3.查半角
select * from 表
where fd collate Chinese_PRC_CS_AS_WS like '%,%'
go

  --删除测试数据
  drop table 表
/*--测试结果

  1.查询大写字母的结果
fd
----------
Aa

  2.查询全角字符的结果
fd
----------
AA
A,A
A,A

  3.查询半角字符的结果
fd
----------
A,A
(所影响的行数为 1 行)
--*/
============================
  --sql7.0,就用下面的方法.
  --如果是全部比较
  --下面是测试
select * from(
select fd='a'
union all select 'A'
) a
where cast(fd as varbinary(8000))=cast('A' as varbinary(8000))

/*--测试结果
fd
----
A
 (所影响的行数为 1 行)
--*/

  --如果是部分匹配,就用charindex:
  --下面是测试
select * from(
select fd='a'
union all select 'A'
union all select 'aAaa'
union all select 'aaaa'
union all select 'cccA'
) a
where charindex(cast('A' as varbinary(8000)),cast(fd as varbinary(8000)))>0
/*--测试结果
fd
----
A
aAaa
cccA
(所影响的行数为 3 行)
--*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值