SQL SERVER 得到汉字首字母函数四版全集 --【叶子】
--创建取汉字首字母函数(第三版)
create function [dbo].[f_getpy_V3]
(
@col varchar(1000)
)
returns varchar(1000)
as
begin
declare @cyc int,@len int,@sql varchar(1000),@char varbinary(20)
select @cyc = 1,@len = len(@col),@sql = ''
while @cyc <= @len
begin
select @char = cast(substring(@col, @cyc, 1) as varbinary)
declare @maco table (bcode varbinary(20),ecode varbinary(20),letter varchar(10))
insert into @maco
select 0XB0A1,0XB0C4,'A' union all
select 0XB0C5,0XB2C0,'B' union all
select 0XB2C1,0XB4ED,'C' union all
select 0XB4EE,0XB6E9,'D' union all
select 0XB6EA,0XB7A1,'E' union all
select 0XB7A2,0XB8C0,'F' union all
select 0XB8C1,0XB9FD,'G' union all
select 0XB9FE,0XBBF6,'H' union all
select 0XBBF7,0XBFA5,'J' union all
select 0XBFA6,0XC0AB,'K' union all
select 0XC0AC,0XC2E7,'L' union all
select 0XC2E8,0XC4C2,'M' union all
select 0XC4C3,0XC5B5,'N' union all
select 0XC5B6,0XC5BD,'O' union all
select 0XC5BE,0XC6D9,'P' union all
select 0XC6DA,0XC8BA,'Q' union all
select 0XC8BB,0XC8F5,'R' union all
select 0XC8F6,0XCBF9,'S' union all
select 0XCBFA,0XCDD9,'T' union all
select 0XCDDA,0XCEF3,'W' union all
select 0XCEF4,0XD1B8,'X' union all
select 0XD1B9,0XD4D0,'Y' union all
select 0XD4D1,0XD7F9,'Z'
select top 1 @sql=@sql+letter from @maco where @char between bcode and ecode
set @cyc = @cyc + 1
end
return @sql
end
go
--创建取汉字首字母函数(第四版)
create function [dbo].[f_getpy_V4]
(
@col varchar(1000)
)
returns varchar(1000)
begin
declare @cyc int,@len int,@sql varchar(1000),@char varbinary(20)
select @cyc = 1,@len = len(@col),@sql = ''
while @cyc <= @len
begin
select @char = cast(substring(@col, @cyc, 1) as varbinary)
if @char>=0XB0A1 and @char<=0XB0C4 set @sql=@sql+'A'
else if @char>=0XB0C5 and @char<=0XB2C0 set @sql=@sql+'B'
else if @char>=0XB2C1 and @char<=0XB4ED set @sql=@sql+'C'
else if @char>=0XB4EE and @char<=0XB6E9 set @sql=@sql+'D'
else if @char>=0XB6EA and @char<=0XB7A1 set @sql=@sql+'E'
else if @char>=0XB7A2 and @char<=0XB8C0 set @sql=@sql+'F'
else if @char>=0XB8C1 and @char<=0XB9FD set @sql=@sql+'G'
else if @char>=0XB9FE and @char<=0XBBF6 set @sql=@sql+'H'
else if @char>=0XBBF7 and @char<=0XBFA5 set @sql=@sql+'J'
else if @char>=0XBFA6 and @char<=0XC0AB set @sql=@sql+'K'
else if @char>=0XC0AC and @char<=0XC2E7 set @sql=@sql+'L'
else if @char>=0XC2E8 and @char<=0XC4C2 set @sql=@sql+'M'
else if @char>=0XC4C3 and @char<=0XC5B5 set @sql=@sql+'N'
else if @char>=0XC5B6 and @char<=0XC5BD set @sql=@sql+'O'
else if @char>=0XC5BE and @char<=0XC6D9 set @sql=@sql+'P'
else if @char>=0XC6DA and @char<=0XC8BA set @sql=@sql+'Q'
else if @char>=0XC8BB and @char<=0XC8F5 set @sql=@sql+'R'
else if @char>=0XC8F6 and @char<=0XCBF9 set @sql=@sql+'S'
else if @char>=0XCBFA and @char<=0XCDD9 set @sql=@sql+'T'
else if @char>=0XCDDA and @char<=0XCEF3 set @sql=@sql+'W'
else if @char>=0XCEF4 and @char<=0XD1B8 set @sql=@sql+'X'
else if @char>=0XD1B9 and @char<=0XD4D0 set @sql=@sql+'Y'
else if @char>=0XD4D1 and @char<=0XD7F9 set @sql=@sql+'Z'
set @cyc = @cyc + 1
end
return @sql
end
go
--创建取汉字首字母函数(第一版)
create function [dbo].[f_getpy_V1] (@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
--创建取汉字首字母函数(第二版)
create function [dbo].[f_getpy_V2](@Str varchar(500)='')
returns varchar(500)
as
begin
declare @strlen int,@return varchar(500),@ii int
declare @n int,@c char(1),@chn nchar(1)
select @strlen=len(@str),@return='',@ii=0
set @ii=0
while @ii<@strlen
begin
select @ii=@ii+1,@n=63,@chn=substring(@str,@ii,1)
if @chn>'z'
select @n = @n +1
,@c = case chn when @chn then char(@n) else @c end
from(
select top 27 * from (
select chn = '吖'
union all select '八'
union all select '嚓'
union all select '咑'
union all select '妸'
union all select '发'
union all select '旮'
union all select '铪'
union all select '丌' --because have no 'i'
union all select '丌'
union all select '咔'
union all select '垃'
union all select '嘸'
union all select '拏'
union all select '噢'
union all select '妑'
union all select '七'
union all select '呥'
union all select '仨'
union all select '他'
union all select '屲' --no 'u'
union all select '屲' --no 'v'
union all select '屲'
union all select '夕'
union all select '丫'
union all select '帀'
union all select @chn) as a
order by chn COLLATE Chinese_PRC_CI_AS
) as b
else set @c='a'
set @return=@return+@c
end
return(@return)
end --思路基本是一样的,但是不同类型导致效率上有差别,这个差别不同环境测试出来的效果竟然不一样。 select dbo.f_getpy_V1('我是一个土生土长的中国人')
select dbo.f_getpy_V2('我是一个土生土长的中国人')
select dbo.f_getpy_V3('我是一个土生土长的中国人')
select dbo.f_getpy_V4('我是一个土生土长的中国人') --我现在测试到的开销百分比是:
--1:2:3:4 对应 18%:38%:44%:0% --如果你感兴趣也可以在本地测试一下,看看执行计划,这4个函数哪个最高效呢?可以把开销百分比留言在下面,谢谢!
SQL SERVER 得到汉字首字母函数四版全集 --【叶子】的更多相关文章
- sql server 提取汉字/数字/字母的方法
sql server 提取汉字/数字/字母的方法 --提取数字 IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL DROP FUNCTION DBO.GET_NU ...
- PHP获取汉字首字母函数
<?php function getFirstCharter($str) { if (empty($str)) { return ''; } $fchar = ord($str{0}); if ...
- mysql获取汉字首字母函数
DELIMITER ;;CREATE FUNCTION `GET_FIRST_PINYIN_CHAR`(PARAM VARCHAR(255)) RETURNS VARCHAR(2) CHARSET u ...
- SQL函数-汉字首字母查询
汉字首字母查询处理用户定义函数 CREATE FUNCTION f_GetPY1(@str nvarchar(4000))RETURNS nvarchar(4000)ASBEGIN DECLARE @ ...
- sql自动生成汉语拼音和首字母函数
1.Sql server自动生成拼音的函数 /* 根据汉字获取全拼 1.生成所有读音临时表 2.根据Chinese_PRC_CS_AS_KS_WS 排序获取读音 */ )) ) as begin ) ...
- MySQL数据库获取多个汉字拼音的首字母函数
需求简介:最近的一个项目,想实现如下图所示的显示效果.很明显,如果能够获取对应的汉字词组中每个汉字的拼音首字母就可以实现了,如果是固定的几组汉字,人为的拼一下就可以 了,不过项目中有多处功能需要这个效 ...
- JS获取汉字首字母
//获取 汉字首字母 function makePy(str) { if (typeof (str) != "string") throw new Error(-1, " ...
- SQL Server 2008性能故障排查(四)——TempDB
原文:SQL Server 2008性能故障排查(四)--TempDB 接着上一章:I/O TempDB: TempDB是一个全局数据库,存储内部和用户对象还有零食表.对象.在SQLServer操作过 ...
- mysql和SqlServer 中取得汉字字段的各汉字首字母
mysql 中取得汉字字段的各汉字首字母 这个转载于http://blog.csdn.net/lky5387/article/details/11973721 DELIMITER ;;CREATE ...
随机推荐
- SQL 实用函数
CREATE FUNCTION [dbo].[varcharmax2table] ( @value VARCHAR(MAX) , @separator VARCHAR(255) ) RETURNS T ...
- eafier 簡單易用 HTML、CSS 網頁編輯器(可自動插入 Tag 標籤)
很久很久以前,一般人要做網頁的話大概都會用 FrontPage 之類的工具,更進階一點的會用 Dreamweaver 等軟體.雖然上述軟體提供很方便的「所見即所得」的編輯預覽模式,但寫出來的網頁程式碼 ...
- SVN发布网站
1.问题 2.分析 如果机器先安装.NET framework,后安装IIS就会出现此问题,原因是.NET framework未注册. 3.注册.NET framework
- JAVA动态加载JAR
// 生成JAR包D:\TestClass.jar package hand.java.loadjar; public class TestClass { private String sayHell ...
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决(转载)
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- Sql Server函数全解<一>字符串函数
阅读目录 1.ASCII()函数 2.CHAR()函数 3.LEFT()函数 4.RIGHT()函数 5.LTRIM()函数 6.RTRIM()函数 7.STR()函数 8.字符串逆序的函数REVER ...
- codeblocks16.01 中配置Opencv3 姿势
VS太大安装时间长,考虑到效率问题,可以使用opencv+codeblocks opencv3没有codeblocks版本,需要自己编译,编译过程略. 已编译版本下载地址 http://pan.bai ...
- 关于python装饰器
关于python装饰器,不是系统的介绍,只是说一下某些问题 1 首先了解变量作用于非常重要 2 其次要了解闭包 def logger(func): def inner(*args, **kwargs) ...
- c语言socket通信
http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html
- html5 历史管理onhashchange和state
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...