當前位置:首頁 » 編程語言 » sql查詢名字首字母
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql查詢名字首字母

發布時間: 2022-05-31 13:42:11

sql中怎麼根據漢字的拼音首字母查詢

---測試數據---
ifobject_id('[pactinfo]')isnotnulldroptable[pactinfo]
go
createtable[pactinfo]([ID]int,[pactname]varchar(4))
insert[pactinfo]
select1,'正常'unionall
select2,'中國'unionall
select3,'做飯'unionall
select4,'加發'

---引用前輩們的一個函數---
createfunctionf_GetPy(@strnvarchar(4000))
returnsnvarchar(4000)
as
begin
declare@strlenint,@renvarchar(4000)
declare@ttable(chrnchar(1)collateChinese_PRC_CI_AS,letternchar(1))
insertinto@t(chr,letter)
select'吖','A'unionallselect'八','B'unionall
select'嚓','C'unionallselect'咑','D'unionall
select'妸','E'unionallselect'發','F'unionall
select'旮','G'unionallselect'鉿','H'unionall
select'丌','J'unionallselect'咔','K'unionall
select'垃','L'unionallselect'嘸','M'unionall
select'拏','N'unionallselect'噢','O'unionall
select'妑','P'unionallselect'七','Q'unionall
select'呥','R'unionallselect'仨','S'unionall
select'他','T'unionallselect'屲','W'unionall
select'夕','X'unionallselect'丫','Y'unionall
select'帀','Z'
select@strlen=len(@str),@re=''
while@strlen>0
begin
selecttop1@re=letter+@re,@strlen=@strlen-1
from@tawherechr<=substring(@str,@strlen,1)
orderbychrdesc
if@@rowcount=0
select@re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1
end
return(@re)
end


---查詢---
select
*
from
[pactinfo]
where
left(dbo.f_GetPy(pactname),1)='Z'

---結果---
IDpactname
-------------------
1正常
2中國
3做飯

(所影響的行數為3行)

Ⅱ 用sql語句怎麼編寫讓名字的首位字母顯示,其他的都用*代替

sqlserver:
select left(name,1)+'**' from table
或者select substring(name,1,1)+'**' from table
這個*不會自動按剩餘數量拼接的,如果你想達到那個效果估計要寫個函數了

Ⅲ 請問sql中如何輸入各個漢字的首字母進行查詢

類似的功能我做過,有表和
存儲過程

漢字
轉pinying,再查詢。

Ⅳ SQL獲取漢字首字母方法

DECLARE @str VARCHAR(100)

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

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


先執行上面的那個函數,然後在執行下面的那個語句,就可以得到你要的結果了。

Ⅳ 用一條sql語句查詢表中欄位的所有首字母大寫

select cname from table where cname in (select initcap(cname) from table);
或者
select cname from table where cname=initcap(cname);

Ⅵ 在sql查詢欄位中怎麼去判斷是以某字母開頭

方法1:用%即可達到。

例如:SELECT*FROMusersWHEREemaillike"%b@email.com%"。

方法2:使用mysql字元串函數find_in_set();

SELECT*FROMusersWHEREfind_in_set('aa@email.com',email);

注意,mysql字元串函數find_in_set(str1,str2)返回str2中str1的位置索引,str2必須被分割成「,」。

方法3:多值模糊查詢,使用mysql正則:REGEXP。

這個方法相當於(比如'%1%'或'%3%'或'%5%')。

從'by_content'中選擇*,其中標題REGEXP'(1|,3|5)'。

(6)sql查詢名字首字母擴展閱讀:

Mysql字元串函數:FIND_IN_SET()

語法:

strlistFIND_IN_SET(STR)

第一個參數STR是要查找的字元串。

第二個參數strlist是要搜索的字元串的逗號分隔列表。

如果字元串STR位於由N個子鏈組成的字元串列表中,則返回值的范圍為1到N。

字元串列表是由','符號分隔的子鏈組成的字元串。如果第一個參數是常量字元串,第二個參數是類型集列,則FIND_IN_SET()函數被優化為使用位。

如果STR不在strlist中,或者strlist是空字元串,則返回值為0。如果任何參數為空,則返回值為空。當第一個參數包含逗號(',')時,此函數將無法正常工作。

Ⅶ 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語句如何查詢首字母大寫

select * from 表 where 欄位 collate chinese_prc_cs_as_ws like 'A%' (查大寫 )
select * from 表 where 欄位 collate chinese_prc_cs_as_ws like 'a%' (查小寫 )
--就是在欄位名後加 collate chinese_prc_cs_as_ws

Ⅸ 如何查詢首字母為『a』的記錄 sql server

查詢首字母為『a』的記錄使用到的是,sql server模糊查詢語句。

一、模糊查詢使用到LIKE 操作符,用於在 WHERE 子句中搜索列中的指定模式。

二、SQL LIKE 操作符語法

SELECTcolumn_name(s)
FROMtable_name
WHEREcolumn_nameLIKEpattern

註:pattern會使用到『%』通配符,表示任意字元或字元串

三、實例演示:

1、實例表格:exp_test