今天遇到一个需求,客户要求在系统客户端选择客户的时候,可以用拼音首拼去快速过滤选择,此时我们在客户表里面加多一个拼音首拼字段CustPY来记录,字段加好了,我们要把所有客户名称的拼音简拼都更新到CustPY字段中。

为此利用以下函数做了个批量更新

执行语句:UPDATE  客户表 SET  CustPY=dbo.[Fn_GetPinYin](客户名称字段)

这样就可以了。需要注意的是:多音字。例如 SELECT dbo.[Fn_GetPinYin]('重庆') 返回ZQ    谨此记录。

-- =============================================
-- 调用:SELECT dbo.[Fn_GetPinYin]('中国')
-- Create date: 2015-01-06
-- Description:    返回汉字拼音首拼
-- =============================================
CREATE  FUNCTION [dbo].[Fn_GetPinYin] (@str NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
    BEGIN
        DECLARE @word NCHAR(1) ,
            @PY NVARCHAR(4000)

        SET @PY=''

        SET @str=REPLACE(@str, '(', '(')
        SET @str=REPLACE(@str, ')', ')')

        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

 

如果朋友们觉得以后可能会用到的话,欢迎转载,当然也可以推荐一下,谢谢。

PS:此博文是利用Windows Live Writer 2012编写,格式效果可能不太好。

SQL标量值函数:返回汉字拼音首拼的更多相关文章

  1. 常用Sql 标量值函数

    1.汉字拼音首字母 /****** Object: UserDefinedFunction [dbo].[fnGetHzPY] Script Date: 08/16/2018 09:04:47 Des ...

  2. sql标量值函数,将汉字转化为拼音,无音标

    USE [db_Test]GO SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO create function [dbo].[fn_GetPinyin]( ...

  3. SQL函数取汉字拼音首字母

    )='') ) as begin ), ) , ,) if @chn > 'z' if( @chn < '八' ) set @c = 'A' else if ( @chn < '嚓' ...

  4. SQL标量值函数:小写金额转大写

    我们日常开发业务系统中,作为统计报表中,特别是财务报表,显示中文金额经常遇到. 转换大小写的方法有很多,以下是从数据库函数方面解决这一问题. 效果如图: 调用:SELECT dbo.[Fn_Conve ...

  5. sql中的表值函数与标量值函数区别与用法

    通俗来讲: 听名字就知道区别了 表值函数返回的是一张表结果,就和一个select查询语句一样,只不过里面带入了参数或者很复杂:标量值函数返回的只是一个值 一 .表值函数又分为内联函数与多语句函数 (1 ...

  6. sqlserver 表值函数与标量值函数

    除了在我们常用的程序开发中要用到函数外,在sql语句中也常用到函数,不论哪种,思想都没有变,都是为了封装,可复用. 创建的方法和整体结构都大体相同,都少不了函数名,函数的形参,返回值等这些. 一.表值 ...

  7. MySQL通过函数获取字符串汉字拼音首字母大写字符串

    DELIMITER $$ DROP FUNCTION IF EXISTS `Fun_GetPY`$$ CREATE FUNCTION `HIS`.`Fun_GetPY` (in_string VARC ...

  8. SQL表值函数和标量值函数的区别

    SQL表值函数和标量值函数的区别 写sql存储过程经常需要调用一些函数来使处理过程更加合理,也可以使函数复用性更强,不过在写sql函数的时候可能会发现,有些函数是在表值函数下写的有些是在标量值下写的, ...

  9. sql 表值函数与标量值函数

    写sql存储过程经常需要调用一些函数来使处理过程更加合理,也可以使函数复用性更强,不过在写sql函数的时候可能会发现,有些函数是在表值函数下写的有些是在标量值下写的,区别是表值函数只能返回一个表,标量 ...

随机推荐

  1. Python 3 并发编程多进程之队列(推荐使用)

    Python 3 并发编程多进程之队列(推荐使用) 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 可以往 ...

  2. <基于Qt与POSIX线程>多线程下载器的简易搭建

    原创博客,转载请联系博主! 本项目已托管到本人Git远程库:https://github.com/yue9944882/Snow 项目目标  Major Functionality 开发环境:  Ce ...

  3. RabbitMQ点对点的模式

    1.点对点模式  一对一模式.  一个生产者投递消息给队列 只能允许有一个消费者进行消费    如果集群的话 会进行均摊消费   服务器配置不一样 均摊就不优了 长连接 不用三次握手之类的 提高传输效 ...

  4. codeforces 707D:Persistent Bookcase

    Description Recently in school Alina has learned what are the persistent data structures: they are d ...

  5. mysql 使用过程中出现问题

    1. mysql_front连接报错,sql执行错误#3167的解决方案 提示:The 'INFORMATION_SCHEMA.SESSION_VARIABLES' feature is disabl ...

  6. sqlserver 函数里并返回一个表格数据拼接的字符串

    Create function [dbo].[GetChildWorkerExtension](     @ChildId int)returns nvarchar(100)asbegin       ...

  7. java:Properties属性文件概念

    java:Properties属性文件概念 在java之前的国际化程序中提出了一个属性文件的概念,属性文件的后缀是:*.properties,那么在java中提供了意个属性文件的专门操作类,Prope ...

  8. Mysql5.5 InnoDB存储引擎配置和优化

    环境为CentOS系统,1G内存,Mysql5.5.30.在/etc/my.cnf内添加: 复制代码代码如下: skip-external-lockingskip-name-resolvemax_co ...

  9. Git_错误_02_error: src refspec master does not match any

    现象:在一个目录下初始化仓库之后,就开始push到github,结果出现了这个错误. 错因:初始化仓库之后,并没有使用git add,git commit 命令将文件添加到git仓库中,所以仓库为空, ...

  10. Java企业微信开发_07_JSSDK多图上传

    一.本节要点 1.1可信域名 所有的JS接口只能在企业微信应用的可信域名下调用(包括子域名),可在企业微信的管理后台“我的应用”里设置应用可信域名.这个域名必须要通过ICP备案,不然jssdk会配置失 ...