查询区分大小写

来源:互联网 发布:java获取工程绝对路径 编辑:程序博客网 时间:2024/05/17 01:59
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

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

--sql2000,就用下面的方法.
--就是在字段名后加collateChinese_PRC_CS_AS_WS


--区分大小写、全半角字符的方法

--测试数据
createtable表(fdvarchar(10))
insertinto表
selectaa='aa'
unionallselect'Aa'
unionallselect'AA'  --全角A
unionallselect'A,A'  --全角A,半角,
unionallselect'A,A' --全角A,全角,
go

--查询
--1.查大写字母
select*from表
wherefdcollateChinese_PRC_CS_AS_WSlike'%A%' 
--就是在字段名后加collateChinese_PRC_CS_AS_WS

--2.查全角
select*from表
wherefdcollateChinese_PRC_CS_AS_WSlike'%A%'

--3.查半角
select*from表
wherefdcollateChinese_PRC_CS_AS_WSlike'%,%'
go

--删除测试数据
droptable表

/*--测试结果

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


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


3.查询半角字符的结果
fd        
----------
A,A

(所影响的行数为1行)
--*/


================================================================

--sql7.0,就用下面的方法.

--如果是全部比较
--下面是测试
select*from(
selectfd='a'
unionallselect'A'
)a
wherecast(fdasvarbinary(8000))=cast('A'asvarbinary(8000))

/*--测试结果
fd  
----
A

(所影响的行数为1行)
--*/

--如果是部分匹配,就用charindex:

--下面是测试
select*from(
selectfd='a'
unionallselect'A'
unionallselect'aAaa'
unionallselect'aaaa'
unionallselect'cccA'
)a
wherecharindex(cast('A'asvarbinary(8000)),cast(fdasvarbinary(8000)))>0

/*--测试结果
fd  
----
A
aAaa
cccA

(所影响的行数为3行)
--*/

 

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击