查询区分大小写
2004-02-09 00:10
405 查看
在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 行)
--*/
--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 行)
--*/
相关文章推荐
- com之套间(Apartment)
- 全新的搜索功能
- Borland CTO Blake Stone & Chuck J gone to Microsoft
- [转帖]Why you can't catch some exceptions
- 给datagrid绑定内容
- InstallShield自定义对话框浅谈(五)
- InstallShield自定义对话框浅谈(二)
- InstallShield自定义对话框浅谈(四)
- 祝贺SharpDevelop中文站开通,让我们共同来挖掘#develop的强大功能吧!
- 怎样用Javascript获得IE临时目录
- Win2000下Bugzilla安装实录(三)
- Win2000下Bugzilla安装实录(二)
- Win2000下Bugzilla安装实录(一)
- InstallShield自定义对话框浅谈(一)
- .net多语言和数据集内多数据表的处理(2)
- .net多语言和数据集内多数据表的处理(1)
- 页面无限跳转间如何保存页面状态 (3)
- 页面无限跳转间如何保存页面状态(2)
- 操作系统学习笔记(一) > 操作系统概论
- EnumChildWindows的威力