Home >Database >Mysql Tutorial >sql sever string interception of Chinese characters

sql sever string interception of Chinese characters

伊谢尔伦
伊谢尔伦Original
2016-11-24 15:30:571398browse

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')

The result a/bc

Attached:

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])
Capital letters: [0x41,0x5a] (or decimal [65, 90])


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn