阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

老司机
查看: 1846|回复: 0

[转帖] 在SQL中取得目标汉字的拼音首字母形成助记码的方法

[复制链接]
  • TA的每日心情

    2019-9-1 10:20
  • 签到天数: 826 天

    [LV.10]以坛为家III

    发表于 2009-5-16 12:52:15 | 显示全部楼层 |阅读模式
    转自金蝶小何的大作:

    背景
    5.1休假期间,有朋友反馈:利用软件的数据交换工具,从Excel中导入基础资料后,系统不能自动形成助记码。

    业务分析

    我们者知道,助记码在一般情况下默认的方案是根据汉字拼音的首字母生成的,因此需要根据导入资料中的汉字名称的第一个拼音字字母,组织形成助记码。
    毕竟在Excel中让他自动形成助码还是不方便的,而每个资料都要去手工录入它的助记码,又不太现实。因此,可以设想一种方案,那就是在资料导入后,通过SQL语句来一次性按规律形成码记码。

    业务实现
    查找了网上的很多资料,获知是不能直接利用SQL命令形成助记码的,需要自建一个函数来计算,才能形成助记码。整理方法如下:
    说明:此函数可应用于任何一个SQL的数据库中,如金蝶KIS专业版、金蝶KIS商贸版
    ----lz_xiaohe----整理于2009-05--一个相当郁闷的假期---
    --01--首先创建一个函数,其功能就是功能是得到汉字拼音首字母
    go
    if exists (select * from sysobjects where name='fun_getPY' and xtype='Fn')
    begin
    drop function fun_getPY
    end
    go
    create function 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 @PY
    end
    go
    --02--函数调用实例
    --函数调用实例1:生成指定字符的助记码
    select dbo.fun_getPY('中华人民共和国') FhelperCode
    --结果为:ZHRMGHG

    --函数调用实例2:生成指定字段的助记码
    --以下结果得到根据第一个汉字字母组合成的记助码
    select  dbo.fun_getPY(Fname) FhelperCode,* from t_account
    order by fnumber

    由于银行的中是多音字,因此把在得到的结果中,按Xing音来形成首字母了。
    对这种情况,需要征集更好的智能判断多音字的方法
    ----函数调用实例3:更新指字段的助记码
    update t_account set FhelperCode=dbo.fun_getPY(Fname)
    where FhelperCode is null

    在使用时,dbo.这几个字每可千万别忘掉了。

    由此,可以延展开它的用法,可以应用到所有需要使用助记码的地方。
    如果还有更多的更好的实现方法,欢迎补充。
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表