--1.查询区分全角与半角字符 --测试数据 declare @t table(col varchar(10)) insert @t select 'aa' union all select 'aa' union all select 'aa' --全角a union all select 'a,a'--全角a,半角逗号(,) union all select 'a,a'--全角a,全角逗号(
--1.查询区分全角与半角字符
--测试数据
declare @t table(col varchar(10))
insert @t select 'aa'
union all select 'aa'
union all select 'aa' --全角a
union all select 'a,a' --全角a,半角逗号(,)
union all select 'a,a' --全角a,全角逗号(,)
--1.查大写字母
select * from @t
where col collate chinese_prc_cs_as_ws like '%a%'
--结果
aa
--2.查全角字母
select * from @t
where col collate chinese_prc_cs_as_ws like '%a%'
--结果
aa
a,a
a,a
--3.查半角逗号(,)
select * from @t
where col collate chinese_prc_cs_as_ws like '%,%'
--结果
a,a
--3.查全角逗号(,)
select * from @t
where col collate chinese_prc_cs_as_ws like '%,%'
go
--结果
a,a
--2 实现全角与半角字符转换的处理函数
create function f_convert(
@str nvarchar(4000), --要转换的字符串
@flag bit --转换标志,0转换成半角,1转换成全角
)returns nvarchar(4000)
as
begin
declare @pat nvarchar(8),@step int,@i int,@spc int
if @flag=0
select @pat=n'%[!-~]%',@step=-65248,
@str=replace(@str,n' ',n' ')
else
select @pat=n'%[!-~]%',@step=65248,
@str=replace(@str,n' ',n' ')
set @i=patindex(@pat collate latin1_general_bin,@str)
while @i>0
select @str=replace(@str,
substring(@str,@i,1),
nchar(unicode(substring(@str,@i,1))+@step))
,@i=patindex(@pat collate latin1_general_bin,@str)
return(@str)
end
go
select [dbo].[f_convert] ('aaa' ,0)
--结果
aaa
作者:http://www.cnblogs.com/aierong/