SQL函数取汉字拼音首字母
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