当前位置:首页 » 编程语言 » sql汉字转首字母
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql汉字转首字母

发布时间: 2023-03-26 23:15:21

‘壹’ sql语句 把用户名中文改成拼音字母

看看这个http://dhjdhja.blog.163.com/blog/static/64762009101684639346/ ps:奶奶的贴一个地址链接还不行什么破论坛,内容又发不完 --sql汉字转拼音
SET ANSI_NULLS ON
GOSET QUOTED_IDENTIFIER ON
GOcreate function [dbo].[fn_GetPinyin](@words nvarchar(2000))
returns varchar(8000)
as
begin
declare @word nchar(1)
declare @pinyin varchar(8000)
declare @i int
declare @words_len int
declare @unicode int
set @i = 1
set @words = ltrim(rtrim(@words))
set @words_len = len(@words)
while (@i <= @words_len) --循环取字符
begin
set @word = substring(@words, @i, 1)
set @unicode = unicode(@word)
set @pinyin = ISNULL(@pinyin+space(1),'')+
(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 'ai',N'靉'
union all select 'an',N'黯'
union all select 'ang',N'醠'
union all select 'ao',N'骜'
union all select 'ba',N'欛'
union all select '',N'瓸' --韛兡瓸
union all select 'ban',N'瓣'
union all select 'bang',N'镑'
union all select 'bao',N'鑤'
union all select 'bei',N'鐾'
union all select 'ben',N'輽'
union all select 'beng',N'镚'
union all select 'bi',N'鼊'
union all select 'bian',N'变'
union all select 'biao',N'鳔'
union all select 'bie',N'彆'
union all select 'bin',N'鬓'
union all select 'bing',N'靐'
union all select 'bo',N'卜'
union all select 'bu',N'簿'
union all select 'ca',N'囃'
union all select 'cai',N'乲' --縩乲
union all select 'can',N'爘'
union all select 'cang',N'賶'
union all select 'cao',N'鼜'
union all select 'ce',N'簎'
union all select 'cen',N'笒'
union all select 'ceng',N'乽' --硛硳岾猠乽
union all select 'cha',N'诧'
union all select 'chai',N'囆'
union all select 'chan',N'颤'
union all select 'chang',N'韔'
union all select 'chao',N'觘'
union all select 'che',N'爡'
union all select 'chen',N'谶'
union all select 'cheng',N'秤'
union all select 'chi',N'鷘'
union all select 'chong',N'铳'
union all select 'chou',N'殠'
union all select 'chu',N'矗'
union all select 'chuai',N'踹'
union all select 'chuan',N'鶨'
union all select 'chuang',N'怆'
union all select 'chui',N'顀'
union all select 'chun',N'蠢'
union all select 'chuo',N'縒'
union all select 'ci',N'嗭' --赐嗭
union all select 'cong',N'謥'
union all select 'cou',N'辏'
union all select 'cu',N'顣'
union all select 'cuan',N'爨'
union all select 'cui',N'臎'
union all select 'cun',N'籿'
union all select 'cuo',N'错'
union all select 'da',N'橽'
union all select 'dai',N'靆'
union all select 'dan',N'饏'
union all select 'dang',N'闣'
union all select '',N'纛'
union all select 'de',N'的'
union all select 'den',N'扽'
union all select 'deng',N'镫'
union all select 'di',N'䗖'
union all select 'dia',N'嗲'
union all select 'dian',N'驔'
union all select 'diao',N'鑃'
union all select 'die',N'嚸' --眰嚸
union all select 'ding',N'顁'
union all select 'diu',N'铥'
union all select 'dong',N'霘'
union all select 'dou',N'鬭'
union all select '',N'蠹'
union all select 'an',N'叾' --簖叾
union all select 'i',N'譵'
union all select 'n',N'踲'
union all select 'o',N'鵽'
union all select 'e',N'鳄'
union all select 'en',N'摁'
union all select 'eng',N'鞥'
union all select 'er',N'樲'
union all select 'fa',N'发'
union all select 'fan',N'瀪'
union all select 'fang',N'放'
union all select 'fei',N'靅'
union all select 'fen',N'鲼'
union all select 'feng',N'覅'
union all select 'fo',N'梻'
union all select 'fou',N'鴀'
union all select 'fu',N'猤' --鳆猤
union all select 'ga',N'魀'
union all select 'gai',N'瓂'
union all select 'gan',N'灨'
union all select 'gang',N'戆'
union all select 'gao',N'锆'
union all select 'ge',N'獦'
union all select 'gei',N'给'
union all select 'gen',N'搄'
union all select 'geng',N'堩' --亘堩啹喼𠮶
union all select 'gong',N'兣' --熕贑兝兣
union all select 'gou',N'购'
union all select 'gu',N'顾'
union all select 'gua',N'诖'
union all select 'guai',N'恠'
union all select 'guan',N'鱹'
union all select 'guang',N'撗'
union all select 'gui',N'鱥'
union all select 'gun',N'謴'
union all select 'guo',N'腂'
union all select 'ha',N'哈'
union all select 'hai',N'饚'

‘贰’ 求注释一小段 sql语句。

你这个代码有点问题,‘M’对应的汉字不是'呒',而应该是口字旁加繁体的无。这个字会被度娘自动转码,所以我截个图:

select基手姿top1letterfrom@twherechr<=[取出汉字]

就可以得到该取出汉字所对应的拼音首字母。


如果上句没有取到值(@@rowcount=0),说明取出的字符比'吖'还要小,也就是说取出的字符不是汉字而是字母、数字或标点。此薯迟时不需要取它的拼音首字母。

‘叁’ 数据库中汉字字段中的内容如何用拼音首字母来查询

或许可这样来处理
1:查询出数据集
循环遍列
同时把数据戚亏库的姓名字段首字母转成汉语拼音后比对和输入的数据是否相同
相同则将其添入另外一个datatable
最辩仔如后把datatable给gridview
作为携启数据源
2:在数据库里加一个冗余字段存姓名的汉字拼音的首字母
最后查询这个字段
把数据绑定到gridview
若有不明
你可以hi我

‘肆’ 如何将sql查询得到数字按大小转换成字母输出

转换的方式有很多种,但是最主要的是你需要制定转换的规则。
比如什么值转换成A,什么值转换成B。这个需要一定的规则,要不然程序是无法知道一个数值要转换成什么字母的。
你可以在sql文里做转换,比如oracle的
decode函数
,以及
sqlserver
的iif函数,只不过这个比较适用于较少
数据项
的转换,你这种情况不太适合用这两个函数进行转换。
你也可以在数据库里做一张配置表,把数值跟字母对应起来,这样查询的时候,根据no值和配置表做一个对应,这样查询出来的结果就是转换后的字母了。
你也可以用xml或者其他文件做一个
配置文件
,把no值作为key,把字母当成value。程序加载时,把它读到
hashtable
里边去,hashtable是k-v方式存储数据的,因此转换的时候,你只要把no值当成key传到hashtable里边去就能获取到响应的字母了。
Private
Sub
Button1_Click(ByVal
sender
As
System.Object,
ByVal
e
As
System.
EventArgs
)
Handles
Button1.Click
hashtable.Add(1001,
"A")
hashtable.Add(1002,
"B")
hashtable.Add(1003,
"C")
hashtable.Add(1004,
"D")
MessageBox
.Show(Exchange(1001))
End
Sub
Public
Function
Exchange(ByVal
source
As
Integer)
As
String
Return
hashtable.Item(source).ToString
End
Function
总之,最主要的是制定一个转换规则。

‘伍’ 请问sql中如何输入各个汉字的首字母进行查询

类似的功能我做过,有表和
存储过程

汉字
转pinying,再查询。

‘陆’ 数据库oracle(plsql)怎么把汉字转化成拼音首字母,如果非汉字字符 (英文or数字),返回原字符不变

oracle汉字转拼音(获得全拼/拼音首字母/拼音截取等)效果如下: Oracle 字符集 GBK 没有问题 , UTF -8 需要修改一下Sql代码
--oracle汉字转拼音 PACKAGE
--1.获得全拼
SELECT GETHZPY.GETHZFULLPY('汉字') FROM DUAL;结果 : HanZi
--2.拼音首字母
SELECT GETHZPY.GETHZPYCAP('汉字') FROM DUAL;结果 : HZ
--3.拼音截取等
SELECT GETHZPY.GETHZPYCAPSUBSTR('汉字', 0, 1) FROM DUAL;结果 : H
代码部分太长挂在附件上 以下代码如果在 PL/SQL Developer 执行的话,选择 Command Window 粘贴.
附件在最下面.
oracle汉字转拼音package_获得全拼——拼音首字母_拼音截取等.zip (35.9 KB)

‘柒’ SQL获取汉字首字母方法

DECLARE @str VARCHAR(100)

SET @str = '汉字的首字母'

SELECT @str AS A, dbo.fun_getPY(@str) AS B


先执行上面的那个函数,然后在执行下面的那个语句,就可以得到你要的结果了。

‘捌’ sql只输出字母为A的表(表名有的是汉字名字)

举个例子,查询客户资料表中的客户简称以字母为A开头的汉字和英语
select * from kfzl where kfjz<'吧' and substring(kfjz,1,1) not in('b','c','d','e','f','g','h','i','j','k','l','m','n,'o','p','q','r','s','t','u','v','w','x','y','z')

英文是按字母排序过来的,中文也是有编码排序的,A开头的汉字肯定在B开头汉字的前面,
所以小于'吧' 编码的就是A字母开头的汉字了

‘玖’ SQL语句提取出中文的拼音首字母

正好最近收藏了一个 你可以看下思路
--将中文字符串转化成文字首拼音的组合
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
--函数调用实例:
select dbo.fun_getPY('中华人民共和国AAA01')

/*

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ZHRMGHGAAA01

(1 行受影响)

*/

‘拾’ SQL 获取名字拼音

做两个函数。一个取汉字拼音首字母,一个取完整拼音

下面代码是晚上搜到的:

--取汉字首字母函数
create function f_GetPy(@str nvarchar(4000))
returns nvarchar(4000)
as
begin
declare @strlen int,@re nvarchar(4000)
declare @t table(chr nchar(1) collate Chinese_PRC_CI_AS,letter nchar(1))
insert into @t(chr,letter)
select '吖','A' union all select '八','B' union all
select '嚓','C' union all select '咑','D' union all
select '妸','E' union all select '发','F' union all
select '旮','G' union all select '铪','H' union all
select '丌','J' union all select '咔','K' union all
select '垃','L' union all select '呒','M' union all
select '拏','N' union all select '噢','O' union all
select '妑','P' union all select '七','Q' union all
select '呥','R' union all select '仨','S' union all
select '他','T' union all select '屲','W' union all
select '夕','X' union all select '丫','Y' union all
select '帀','Z'
select @strlen=len(@str),@re=''
while @strlen>0
begin
select top 1 @re=letter+@re,@strlen=@strlen-1
from @t a where chr<=substring(@str,@strlen,1)
order by chr desc
if @@rowcount=0
select @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1
end
return(@re)
end
go

--使用
select id,name,dbo.f_GetPy(name) from mytable

取完整拼音的跟上述f_GetPy函数类似,只是临时表要多写很多行,拼音有多少种组合就写多少行,大概六七百行吧。你自己找找相关的其他语言转拼音全码的代码,实现方式是相通的。