Username: Password:

查询区分大小写-数据库专栏,SQL Server
来源:作者: 发布时间:2007-12-26 01:57:40


在sql2000和7.0的查询语句中,区分大写的查询方法

--sql2000,就用下面的方法.
--就是在字段名后加 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 行)
--*/

 

 

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅