SQL Server 如何提取汉字首字母

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server 如何提取汉字首字母相关的知识,希望对你有一定的参考价值。

SQL Server数据库内存放如下数据

河南-轴承-22
河北-螺丝-23
江苏-钢板-23

我想通过查询得到这样的数据
hn-zc-22
hb-ls-23
js-gb-23

请问SQL语句该如何写啊!!

代码如下:
USE [database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[getPinYin] (@str varchar(500) = '')
RETURNS varchar(500) AS
BEGIN
Declare @strlen int,
@return varchar(500),
@ii int,
@c char(1),
@chn nchar(1)
--//初始化变量
Declare @pytable table(
chn char(2) COLLATE Chinese_PRC_CS_AS NOT NULL,
py char(1) COLLATE Chinese_PRC_CS_AS NULL,
PRIMARY KEY (chn)
)
insert into @pytable values('吖', 'A')
insert into @pytable values('八', 'B')
insert into @pytable values('嚓', 'C')
insert into @pytable values('咑', 'D')
insert into @pytable values('妸', 'E')
insert into @pytable values('发', 'F')
insert into @pytable values('旮', 'G')
insert into @pytable values('铪', 'H')
insert into @pytable values('丌', 'I')
insert into @pytable values('丌', 'J')
insert into @pytable values('咔', 'K')
insert into @pytable values('垃', 'L')
insert into @pytable values('呒', 'M')
insert into @pytable values('拏', 'N')
insert into @pytable values('噢', 'O')
insert into @pytable values('妑', 'P')
insert into @pytable values('七', 'Q')
insert into @pytable values('呥', 'R')
insert into @pytable values('仨', 'S')
insert into @pytable values('他', 'T')
--insert into @pytable values('屲', 'U')
--insert into @pytable values('屲', 'V')
insert into @pytable values('屲', 'W')
insert into @pytable values('夕', 'X')
insert into @pytable values('丫', 'Y')
insert into @pytable values('帀', 'Z')
select @strlen = len(@str), @return = '', @ii = 0
//循环整个字符串,用拼音的首字母替换汉字
while @ii < @strlen
begin
select @ii = @ii + 1, @chn = substring(@str , @ii, 1)
if @chn > 'z' --//检索输入的字符串中有中文字符
SELECT @c = max(py)
FROM @pytable
where chn <= @chn
else
set @c=@chn

set @return=@return+@c
end
return @return
END
参考技术A

这个我以前写过, 要用 C# 来写, 然后 发布到 SQL Server 上面去。

你要是会 C# 的话, 倒可以尝试尝试, 否则就不必下载附件了。


那个项目还引用了  Microsoft.International.Converters.PinYinConverter 这个类库。 

你可能还要去微软网站找来下载一下。

或者用其他的算法, 通过汉字, 返回拼音的。


参考技术B 思路比较明确,建立获取汉字首字母拼音的函数,然后调用自定义的函数,执行查询,就可以得到结果。 参考技术C 这样:
select dbo.getpyFirst(fieldname) from tablename
但需要先建立以下函数:
create function GetPyFirst
(@Str varchar(500)='')
returns varchar(500)
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=@chn
set @return=@return
end
return @return
end

GO本回答被提问者和网友采纳

excel提取汉字拼音首字母

本文转载:http://jingyan.baidu.com/article/63acb44adca44461fcc17e85.html

利用Excel表格中的宏,轻松提取首字母

方法/步骤

1.启动Excel 2003(其它版本请仿照操作),打开相应的工作表;

2.执行“工具→宏→Visual Basic编辑器”命令(或者直接按“Alt+F11”组合键),进入Visual Basic编辑状态;

3.执行“插入→模块”命令,插入一个新模块。再双击插入的模块,进入模块代码编辑状态;

4.将代码输入其中;

Function pinyin(p As String) As String

i = Asc(p)

Select Case i

Case -20319 To -20284: pinyin = "A"

Case -20283 To -19776: pinyin = "B"

Case -19775 To -19219: pinyin = "C"

Case -19218 To -18711: pinyin = "D"

Case -18710 To -18527: pinyin = "E"

Case -18526 To -18240: pinyin = "F"

Case -18239 To -17923: pinyin = "G"

Case -17922 To -17418: pinyin = "H"

Case -17417 To -16475: pinyin = "J"

Case -16474 To -16213: pinyin = "K"

Case -16212 To -15641: pinyin = "L"

Case -15640 To -15166: pinyin = "M"

Case -15165 To -14923: pinyin = "N"

Case -14922 To -14915: pinyin = "O"

Case -14914 To -14631: pinyin = "P"

Case -14630 To -14150: pinyin = "Q"

Case -14149 To -14091: pinyin = "R"

Case -14090 To -13319: pinyin = "S"

Case -13318 To -12839: pinyin = "T"

Case -12838 To -12557: pinyin = "W"

Case -12556 To -11848: pinyin = "X"

Case -11847 To -11056: pinyin = "Y"

Case -11055 To -2050: pinyin = "Z"

Case Else: pinyin = p

End Select

End Function

Function getpy(str)

For i = 1 To Len(str)

getpy = getpy & pinyin(Mid(str, i, 1))

Next i

End Function

5.代码输入完成后,关闭Visual Basic编辑窗口,返回Excel编辑状态;

6.自定义函数就可以用了,如:选中A2单元格,输入公式:=getpy(A2)。

以上是关于SQL Server 如何提取汉字首字母的主要内容,如果未能解决你的问题,请参考以下文章

JAVA 汉字首字母排序

excel提取汉字拼音首字母

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

sql 获取汉字首字母的函数.SQL

SQL Server 汉字转拼音字母

SQL根据汉字首字母排序