目前工作中遇到一个情况,需要将SQL Server中的一个字段提取拼音的首字母,字段由汉字、英文、数字以及“-”构成,百度了一堆,找到如下方法,记录一下,以备后用!

  首先建立一个函数

--生成拼音首码
CREATE function fn_GetPy(@str nvarchar(4000))
returns nvarchar(4000)
--WITH ENCRYPTION
as
begin
declare @intLen int
declare @strRet nvarchar(4000)
declare @temp nvarchar(100)
set @intLen = len(@str)
set @strRet = ''
while @intLen > 0
begin
set @temp = ''
select @temp = case
when substring(@str,@intLen,1) >= '帀' then 'Z'
when substring(@str,@intLen,1) >= '丫' then 'Y'
when substring(@str,@intLen,1) >= '夕' then 'X'
when substring(@str,@intLen,1) >= '屲' then 'W'
when substring(@str,@intLen,1) >= '他' then 'T'
when substring(@str,@intLen,1) >= '仨' then 'S'
when substring(@str,@intLen,1) >= '呥' then 'R'
when substring(@str,@intLen,1) >= '七' then 'Q'
when substring(@str,@intLen,1) >= '妑' then 'P'
when substring(@str,@intLen,1) >= '噢' then 'O'
when substring(@str,@intLen,1) >= '拏' then 'N'
when substring(@str,@intLen,1) >= '嘸' then 'M'
when substring(@str,@intLen,1) >= '垃' then 'L'
when substring(@str,@intLen,1) >= '咔' then 'K'
when substring(@str,@intLen,1) >= '丌' then 'J'
when substring(@str,@intLen,1) >= '铪' then 'H'
when substring(@str,@intLen,1) >= '旮' then 'G'
when substring(@str,@intLen,1) >= '发' then 'F'
when substring(@str,@intLen,1) >= '妸' then 'E'
when substring(@str,@intLen,1) >= '咑' then 'D'
when substring(@str,@intLen,1) >= '嚓' then 'C'
when substring(@str,@intLen,1) >= '八' then 'B'
when substring(@str,@intLen,1) >= '吖' then 'A'
else rtrim(ltrim(substring(@str,@intLen,1)))
end
--对于汉字特殊字符,不生成拼音码
if (ascii(@temp)>127) set @temp = ''
--对于英文中小括号,不生成拼音码
if @temp = '(' or @temp = ')' set @temp = ''
select @strRet = @temp + @strRet
set @intLen = @intLen - 1
end
return lower(@strRet)
end

执行语句

SELECT    需转换中文字段, dbo.fn_GetPy(中文字段) AS 列别名
FROM 表名称

感谢:Luckeryin

SQL Server 字段提取拼音首字母的更多相关文章

  1. sql server 汉字转拼音首字母

    create function fun_getPY ( @str nvarchar(4000) ) returns nvarchar(4000) as begin declare @word ncha ...

  2. mysql中文字段转拼音首字母,以及中文拼音模糊查询

    创建存储过程,将中文字段转拼音首字母 CREATE DEFINER=`root`@`%` FUNCTION `fristPinyin`(P_NAME VARCHAR(255)) RETURNS var ...

  3. C#&Sql获取中文字符拼音首字母的方法

    C#获取字符拼音首字母,可以存储在数据库中以备将来按字母搜索的需求. public static string GetAc(string s) { try { string temp = Servic ...

  4. sql获取汉字的拼音首字母

    if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and xtype in (N'FN' ...

  5. SQL 汉字转换成拼音首字母 首字母查

    -- ============================================= -- 功能:汉字转换成拼音首字母 首字母查 -- ========================== ...

  6. 算法笔记_232:提取拼音首字母(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 在很多软件中,输入拼音的首写字母就可以快速定位到某个词条.比如,在铁路售票软件中,输入: “bj”就可以定位到“北京”.怎样在自己的软件中实现这个功 ...

  7. sql获取汉字的拼音首字母的函数

    ql获取汉字的拼音首字母   if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and ...

  8. Java实现提取拼音首字母

    在很多软件中,输入拼音的首写字母就可以快速定位到某个词条.比如,在铁路售票软件中,输入: "bj"就可以定位到"北京".怎样在自己的软件中实现这个功能呢?问题的 ...

  9. sql语句 汉字转拼音首字母

    (1)------------------------------------------------------------------------------------------------- ...

随机推荐

  1. openresty开发系列33--openresty执行流程之3重写rewrite和重定向

    openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令语法:i ...

  2. (转)CentOS 7.6 上编译安装httpd 2.4.38

    原文:https://www.s4lm0x.com/archives/40.html https://www.cnblogs.com/sunshine-H/p/8110608.html----超详细 ...

  3. MySQL数据库事务的四大特性以及事务的隔离级别

    一.事务的四大特性(ACID) 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: 1.原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚,因 ...

  4. k8s记录-yum本地仓库部署

    #1.安装插件yum install -y yum-plugin-downloadonly createrepo rsync #2.创建仓库目录mkdir -p /mirrors/centos#3.下 ...

  5. windows系统下同时安装mysql5.5和8.0.11

    前提:电脑已安装5.5,增安装8.0.11 zip版本 1.官网下载mysql10.8.0.11 —找到mysql community server 为下载页面URL:https://dev.mysq ...

  6. (三)用CONCAT 函数 拼接字段

    一.将两个列拼接成一个列 数据源 select CONCAT(TRIM(username),'(',locaiton,')') from user2 解释: TRIM()函数用于去除字符串左右两边的空 ...

  7. consul ACL 配置范例

    service "dashboard" { policy = "write" } service "dashboard-sidecar-proxy&q ...

  8. Python (Windows) - ImportError: No module named win32service

    ImportError: No module named win32service you have to install pypiwin32

  9. Mac和window实现双向数据传输

    Mac和window实现双向数据传输 总体步骤:第一步,在window上设置开发访问权限,然后选择要共享的磁盘或者文件夹第二步,在Mac上使用 Finder里面的网络,command+K,选择一个IP ...

  10. 「模拟赛20191019」C 推式子+贪心+树状数组

    题目描述 给定一棵\(n\)个点的有根树,根节点编号为\(1\),点有点权. 定义\(d(v)\)表示\(v\)到\(1\)的路径上的边数. 定义\(f(v,u)\)在\(v<u\)且\(v\) ...