Recently, I need to intercept Chinese characters from SQL strings, use the unicode function to determine the unicode encoding of the characters, and filter out non-Chinese characters according to the encoding range.
Written as a function
/*@str Need to get the string of Chinese characters*/
create function CharRep(@str nvarchar(200))
returns nvarchar(200)
as
begin
declare @i int , @char nvarchar(1), @zh nvarchar(200)
set @i = 1
set @zh = ''
while @i <= len(@str)
begin
set @char = substring(@str , @i, 1)
if unicode(@char) between 19968 And 40869
set @zh = @zh + @char
set @i = @i + 1
end
return @zh
end
Execute select dbo.CharRep('Nonghao a/bc Nonghao')
Result a/bc
Attachment:
unicode encoding range:
Chinese characters: [0x4e00,0x9fa5] (or decimal [19968, 40869])
Numbers: [0x30,0x39] (or decimal [48, 57]) Search
Lowercase letters: [0x61,0x7a] (or decimal [97, 122])
Uppercase letters: [0x41,0x5a] (or decimal [65, 90] )