Ⅰ 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