SqlServer如何判断字段中是否含有汉字?

时间:2021-08-26 04:58:55
--/*
--unicode编码范围:
--汉字:[0x4e00,0x9fa5](或十进制[19968,40869])
--数字:[0x30,0x39](或十进制[48, 57])
--小写字母:[0x61,0x7a](或十进制[97, 122])
--大写字母:[0x41,0x5a](或十进制[65, 90])
--根据编码范围来判断
--*/
--创建 create proc p_A_VIC
as
declare @count int
declare @i int
declare @text nvarchar(50)
set @i = 0
set @count = (select COUNT (*) from table )
while(@i < @count )
begin
set @i +=1
--sid代表有一定循环规律的,若是无序的可以添加一个序列(Row_Number() OVER ---)。
--select * from (SELECT *, Row_Number() OVER ( ORDER BY [sid] ) num FROM s--table ) as s where num = 3
set @text = (select a from table where [sid] = @i)
if unicode(@text) between 19968 And 40869 or unicode(@text) between 97 And 122 or unicode('a') between 65 And 90
begin
print 0
end
else
print @text
end
--执行
exec p_A_VIC

实例1:

---由于某些原因HouseName 字段存入了GUID,为了区分
SELECT HouseName FROM ZSGYTD_HouseInfo
SELECT HouseName FROM ZSGYTD_HouseInfo WHERE UNICODE(HouseName) BETWEEN 19968 AND 40869

执行结果:

SqlServer如何判断字段中是否含有汉字?

实例2:

----将上述存入ZSGYTD_Estate 表ID的houseName进行左连接,获取到对应的Name
SELECT h.HouseName,
CASE WHEN UNICODE(h.HouseName) BETWEEN 19968 AND 40869 then h.HouseName
ELSE e.Name
END housename2
FROM ZSGYTD_HouseInfo h
LEFT JOIN ZSGYTD_Estate e ON CONVERT(varchar(50),e.ID)=h.HouseName and e.IsDeleted=0 select*from ZSGYTD_Estate

执行结果:

SqlServer如何判断字段中是否含有汉字?