sqlserver 快速生成汉字的首拼字母的函数(经典)

来源:asp之家 时间:2012-06-06 20:16:41 

代码如下:


USE [tempdb]
GO
/****** Object: UserDefinedFunction [dbo].[fun_getPY] Script Date: 05/23/2012 18:03:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
获取汉字的首拼音
如果是非汉字字符
*/
ALTER function [dbo].[fun_getPY]
(
@str nvarchar(4000)
)
returns nvarchar(4000)
as
begin
declare @word nchar(1),@PY nvarchar(4000)
set @PY=''
while len(@str)>0
begin
set @word=left(@str,1)
--如果非汉字字符,返回原字符
set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
then (
select top 1 PY
from
(
select 'A' as PY,N'驁' as word
union all select 'B',N'簿'
union all select 'C',N'錯'
union all select 'D',N'鵽'
union all select 'E',N'樲'
union all select 'F',N'鰒'
union all select 'G',N'腂'
union all select 'H',N'夻'
union all select 'J',N'攈'
union all select 'K',N'穒'
union all select 'L',N'鱳'
union all select 'M',N'旀'
union all select 'N',N'桛'
union all select 'O',N'漚'
union all select 'P',N'曝'
union all select 'Q',N'囕'
union all select 'R',N'鶸'
union all select 'S',N'蜶'
union all select 'T',N'籜'
union all select 'W',N'鶩'
union all select 'X',N'鑂'
union all select 'Y',N'韻'
union all select 'Z',N'咗'
) T
where word>=@word collate Chinese_PRC_CS_AS_KS_WS
order by PY ASC
)
else @word
end)
set @str=right(@str,len(@str)-1)
end
return upper(@PY)
end


调用如下:


select dbo.[fun_getPY]('中国人') 首拼


结果:
ZGR

标签:汉字,首拼字母
0
投稿

猜你喜欢

  • ORACLE常见错误代码的分析与解决(三)

    2010-07-31 12:45:00
  • 企业网站FLASH引导页存在的意义

    2008-06-04 11:09:00
  • 使用SSIS创建同步数据库数据任务的方法

    2012-11-30 19:53:44
  • Dreamweaver技巧十二招

    2009-07-05 18:53:00
  • css清除浮动的最优方法

    2008-04-25 22:33:00
  • ASP中页面限权访问的几种方法

    2007-12-13 06:53:00
  • 两个css郁闷的发现

    2007-12-16 15:31:00
  • 如何防止Application对象在多线程访问中出现错误?

    2009-11-22 19:18:00
  • asp生成不需要数据库的中奖码

    2008-07-18 12:31:00
  • XML入门的常见问题(一)

    2008-09-05 17:20:00
  • 支付宝lab logo设计创意发想

    2009-11-12 12:44:00
  • Python保存数据至MySQL时中文问题

    2011-02-23 12:06:00
  • ImageMagicK convert crop参数说明

    2008-10-21 12:46:00
  • MYSQL启用日志和查看日志

    2010-12-03 16:24:00
  • 动态载入asp树源码

    2007-09-06 19:34:00
  • 用mysql做站点时怎样记录未知错误的发生

    2009-01-14 13:16:00
  • ASP中利用正则表达式实现论坛UBB代码转换

    2008-02-29 11:49:00
  • 如何列出SQL数据库中的存储过程?

    2010-01-12 19:58:00
  • 利用sys.sysprocesses检查SqlServer的阻塞和死锁

    2011-11-03 17:24:11
  • 使用FORFILES命令来删除SQLServer备份的批处理

    2012-05-08 06:47:06
  • asp之家 网络编程 m.aspxhome.com