TomSun

享受编程的快乐!Attitude is everything!

导航

sql函数:成生文字的第一个英文字母

Posted on 2005-05-10 09:09  TomSun  阅读(429)  评论(0)    收藏  举报

 

 1CREATE  function  udf_GetFirst_Py 
 2(@Str  varchar(500)=''
 3returns  varchar(500
 4as 
 5begin 
 6 Declare @strlen  int
 7  @return  varchar(500), 
 8                @ii  int 
 9        Declare @n  int
10                @c  char(1), 
11                @chn  nchar(1
12 --//初始化变量 
13        Select  @strlen=len(@str),@return='',@ii=0 
14 --//确认输入不为空 
15        if @strlen=0 
16 begin 
17  select @return='Please input chatacter!' 
18  return(@return
19 end 
20        --//循环整个字符串,用拼音的首字母替换汉字 
21           while  @ii<@strlen 
22           begin 
23                       select  @ii=@ii+1,@n=63,@chn=substring(@str,@ii,1
24                       if  @chn>'z'  --//检索输入的字符串中有中文字符 
25                       select  @n=@n+1 ,@c = case  chn  when  @chn  then 
26char(@n)  else  @c  end 
27                                   from(   select  top  27  *  from  ( 
28                                                           select  chn  =  ' 
29' 
30                                                           union  all 
31select  '' 
32                                                           union  all 
33select  '' 
34                                                           union  all 
35select  '' 
36                                                           union  all 
37select  '' 
38                                                           union  all 
39select  '' 
40                                                           union  all 
41select  '' 
42                                                           union  all 
43select  '' 
44                                                           union  all 
45select  '' 
46                                                           union  all 
47select  '' 
48                                                           union  all 
49select  '' 
50                                                           union  all 
51select  '' 
52                                                           union  all 
53select  '' 
54                                                           union  all 
55select  '' 
56                                                           union  all 
57select  '' 
58                                                           union  all 
59select  '' 
60                                                           union  all 
61select  '' 
62                                                           union  all 
63select  '' 
64                                                           union  all 
65select  '' 
66                                                           union  all 
67select  '' 
68                                                           union  all 
69select  '' 
70                                                           union  all 
71select  '' 
72                                                           union  all 
73select  '' 
74                                                           union  all 
75select  '' 
76                                                           union  all 
77select  '' 
78                                                           union  all 
79select  '' 
80                                                           union  all 
81select  @chn)  as  a 
82                                               order  by  chn  COLLATE 
83Chinese_PRC_CI_AS 
84                                   )  as  b 
85                       else  set  @c=@chn 
86                       set  @return=@return+@c 
87           end 
88           return(@return
89end