cndaliu.com
专注于技术,切不可沉湎于技术
<2009年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

文章分类

友情链接

用SQL脚本获取汉字首字母

类别:数据库 - Sql Server   时间:2008-8-20 13:39:41   点击:426   标签:SQL 脚本 获取 汉字 首字母

方法一:
用SQL脚本获取汉字首字母,代码如下:
CREATE FUNCTION GetFirstLetter(@str nvarchar(4000))
 RETURNS nvarchar(4000)
 AS
 BEGIN
     DECLARE @py TABLE(
         ch char(1),
         hz1 nchar(1) COLLATE Chinese_PRC_CS_AS_KS_WS,
         hz2 nchar(1) COLLATE Chinese_PRC_CS_AS_KS_WS)
     INSERT @py SELECT 'A',N'吖',N'鏊'
    UNION  ALL SELECT 'B',N'八',N'簿'
    UNION  ALL SELECT 'C',N'嚓',N'错'
    UNION  ALL SELECT 'D',N'哒',N'跺'
    UNION  ALL SELECT 'E',N'屙',N'贰'
    UNION  ALL SELECT 'F',N'发',N'馥'
    UNION  ALL SELECT 'G',N'旮',N'过'
    UNION  ALL SELECT 'H',N'铪',N'蠖'
    UNION  ALL SELECT 'J',N'丌',N'竣'
    UNION  ALL SELECT 'K',N'咔',N'廓'
    UNION  ALL SELECT 'L',N'垃',N'雒'
    UNION  ALL SELECT 'M',N'妈',N'穆'
    UNION  ALL SELECT 'N',N'拿',N'糯'
    UNION  ALL SELECT 'O',N'噢',N'沤'
    UNION  ALL SELECT 'P',N'趴',N'曝'
    UNION  ALL SELECT 'Q',N'七',N'群'
    UNION  ALL SELECT 'R',N'蚺',N'箬'
    UNION  ALL SELECT 'S',N'仨',N'锁'
    UNION  ALL SELECT 'T',N'他',N'箨'
    UNION  ALL SELECT 'W',N'哇',N'鋈'
    UNION  ALL SELECT 'X',N'夕',N'蕈'
    UNION  ALL SELECT 'Y',N'丫',N'蕴'
    UNION  ALL SELECT 'Z',N'匝',N'做'
    DECLARE @i int
    SET @i=PATINDEX('%[吖-做]%' COLLATE Chinese_PRC_CS_AS_KS_WS,@str)
    WHILE @i>0
        SELECT @str=REPLACE(@str,SUBSTRING(@str,@i,1),ch)
            ,@i=PATINDEX('%[吖-做]%' COLLATE Chinese_PRC_CS_AS_KS_WS,@str)
        FROM @py
        WHERE SUBSTRING(@str,@i,1) BETWEEN hz1 AND hz2

    RETURN(@str)
END

方法二:
--获取汉字首字母
CREATE FUNCTION GetFirstLetter(@RealName VarChar(30))
RETURNS VarChar(2)
AS
BEGIN
 declare @FirstLetter VarChar(2)
 declare @Letter varbinary(2)
 set @Letter = cast(@RealName as varbinary(2))
 select @FirstLetter = case when @Letter >= 0xb0a1 and @Letter < 0xb0c5 then 'a'
  when @Letter >= 0xb0c5 and @Letter < 0xb2c1 then 'b'
  when @Letter >= 0xb2c1 and @Letter < 0xb4ef then 'c'
  when @Letter >= 0xb4ef and @Letter < 0xb6ea then 'd'
  when @Letter >= 0xb6ea and @Letter < 0xb7a2 then 'e'
  when @Letter >= 0xb7a2 and @Letter < 0xb8c1 then 'f'
  when @Letter >= 0xb8c1 and @Letter < 0xb9fe then 'g'
  when @Letter >= 0xb9fe and @Letter < 0xbbf7 then 'h'
  when @Letter >= 0xbbf7 and @Letter < 0xbfa6 then 'j'
  when @Letter >= 0xbfa6 and @Letter < 0xc0ac then 'k'
  when @Letter >= 0xc0ac and @Letter < 0xc2e9 then 'l'
  when @Letter >= 0xc2e9 and @Letter < 0xc4c3 then 'm'
  when @Letter >= 0xc4c3 and @Letter < 0xc5b6 then 'n'
  when @Letter >= 0xc5b6 and @Letter < 0xc5be then 'o'
  when @Letter >= 0xc5be and @Letter < 0xc6da then 'p'
  when @Letter >= 0xc6da and @Letter < 0xc8bb then 'q'
  when @Letter >= 0xc8bb and @Letter < 0xc8f6 then 'r'
  when @Letter >= 0xc8f6 and @Letter < 0xcbfa then 's'
  when @Letter >= 0xcbfa and @Letter < 0xcdda then 't'
  when @Letter >= 0xcdda and @Letter < 0xcef4 then 'w'
  when @Letter >= 0xcef4 and @Letter < 0xd1b9 then 'x'
  when @Letter >= 0xd1b9 and @Letter < 0xd4d1 then 'y'
  when @Letter >= 0xd4d1 and @Letter < 0xd7f9 then 'z'
 End
 Return(@FirstLetter)
END

相关文章


发表评论


  • 姓名:
  • 评论:
  •          
Copyright © cndaliu.com 2007-2010, Powered by 52DotNet.com 苏ICP备07506889号