Recently, we need to intercept Chinese characters in a SQL string, use Unicode functions to determine the Unicode encoding of characters, and filter out non-Chinese characters according to the encoding range.
Wrote a function.
/* @str need to get a string of Kanji */
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
Executes select dbo. Charrep (' Lennon good A/BC Lennon good ')
Result A/BC
Report:
Unicode encoding Range:
Kanji: [0x4e00,0x9fa5] (or decimal [19968,40869])
Number: [0x30,0x39] (or decimal [48, 57]) search
lowercase letters: [0x61,0x7a] (or decimal [97, 122])
Capital letters: [0X41,0X5A] (or decimal [65, 90])
SQL Sever string Intercept kanji