SQL函数取汉字拼音首字母

时间:2022-05-27 08:04:34
 1 CREATE function f_GetFirstPy(@str varchar(50)='')
 2     returns char(1)
 3 as
 4 begin
 5     declare @n int, @c char(1), @chn nchar(1)
 6     select @n=63, @chn=substring(@str,1,1)
 7     if @chn > 'z'
 8         if( @chn < '' ) set @c = 'A'
 9         else if ( @chn < '' ) set @c = 'B'
10         else if ( @chn < '' ) set @c = 'C'
11         else if ( @chn < '' ) set @c = 'D'
12         else if ( @chn < '' ) set @c = 'E'
13         else if ( @chn < '' ) set @c = 'F'
14         else if ( @chn < '' ) set @c = 'G'
15         else if ( @chn < '' ) set @c = 'H'
16         else if ( @chn < '' ) set @c = 'I'
17         else if ( @chn < '' ) set @c = 'J'
18         else if ( @chn < '' ) set @c = 'K'
19         else if ( @chn < '' ) set @c = 'L'
20         else if ( @chn < '' ) set @c = 'M'
21         else if ( @chn < '' ) set @c = 'N'
22         else if ( @chn < '' ) set @c = 'O'
23         else if ( @chn < '' ) set @c = 'P'
24         else if ( @chn < '' ) set @c = 'Q'
25         else if ( @chn < '' ) set @c = 'R'
26         else if ( @chn < '' ) set @c = 'S'
27         else if ( @chn < '' ) set @c = 'T'
28         else if ( @chn < '' ) set @c = 'U'
29         else if ( @chn < '' ) set @c = 'V'
30         else if ( @chn < '' ) set @c = 'W'
31         else if ( @chn < '' ) set @c = 'X'
32         else if ( @chn < '' ) set @c = 'Y'
33         else set @c = 'Z'
34     else if @chn <= 'z' and @chn >= 'a'
35         set @c = upper(@chn)
36     else if @chn <= 'Z' and @chn >= 'A'
37         set @c = @chn
38     else
39         set @c = ''
40     return (@c)
41 end