當前位置:首頁 » 編程語言 » 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函數類似,只是臨時表要多寫很多行,拼音有多少種組合就寫多少行,大概六七百行吧。你自己找找相關的其他語言轉拼音全碼的代碼,實現方式是相通的。