SqlServer如何判斷欄位中是否含有漢字?
阿新 • • 發佈:2018-12-05
--/* --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 WHEREUNICODE(HouseName) BETWEEN 19968 AND 40869
執行結果:
例項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
執行結果: