请教关于中文字符串拼音首字母的问题

safematch 2005-04-06 02:24:59
想做成这样的效果,用户可以点击一个字母来查询出相应的拼音首字母的字段,请问高手,sql查询语句要怎么写?
...全文
139 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
rickjelly2004 2005-04-06
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3337/3337412.xml?temp=.5790369


刚刚整理好-汉字转拼音缩写的函数(C#)
在CSDN上找了一下,没有找一完整的转换函数,特在前人基础上整理了一下,接下来的项目中有可能用到。感谢bugfree(八个飞飞)。

/// <summary>
/// 汉字转拼音缩写
/// Code By MuseStudio@hotmail.com
/// 2004-11-30
/// </summary>
/// <param name="str">要转换的汉字字符串</param>
/// <returns>拼音缩写</returns>
public string GetPYString(string str)
{
string tempStr = "";
foreach(char c in str)
{
if((int)c >= 33 && (int)c <=126)
{//字母和符号原样保留
tempStr += c.ToString();
}
else
{//累加拼音声母
tempStr += GetPYChar(c.ToString());
}
}
return tempStr;
}

/// <summary>
/// 取单个字符的拼音声母
/// Code By MuseStudio@hotmail.com
/// 2004-11-30
/// </summary>
/// <param name="c">要转换的单个汉字</param>
/// <returns>拼音声母</returns>
public string GetPYChar(string c)
{
byte[] array = new byte[2];
array = System.Text.Encoding.Default.GetBytes(c);
int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));

if ( i < 0xB0A1) return "*";
if ( i < 0xB0C5) return "a";
if ( i < 0xB2C1) return "b";
if ( i < 0xB4EE) return "c";
if ( i < 0xB6EA) return "d";
if ( i < 0xB7A2) return "e";
if ( i < 0xB8C1) return "f";
if ( i < 0xB9FE) return "g";
if ( i < 0xBBF7) return "h";
if ( i < 0xBFA6) return "g";
if ( i < 0xC0AC) return "k";
if ( i < 0xC2E8) return "l";
if ( i < 0xC4C3) return "m";
if ( i < 0xC5B6) return "n";
if ( i < 0xC5BE) return "o";
if ( i < 0xC6DA) return "p";
if ( i < 0xC8BB) return "q";
if ( i < 0xC8F6) return "r";
if ( i < 0xCBFA) return "s";
if ( i < 0xCDDA) return "t";
if ( i < 0xCEF4) return "w";
if ( i < 0xD1B9) return "x";
if ( i < 0xD4D1) return "y";
if ( i < 0xD7FA) return "z";

return "*";
}



版权声明:CSDN是本Blog托管服务提供商。如本文牵涉版权问题,CSDN不承担相关责任,请版权拥有者直接与文章作者联系解决。



yjk1028why 2005-04-06
  • 打赏
  • 举报
回复
where c1 like A%;查找首字为A的字符串
safematch 2005-04-06
  • 打赏
  • 举报
回复
是这样,我想达到这样的效果,如果点A,sql语句应该为select * from t1 where c1的拼音首字母为A
但是不知道条件部分怎么办,请高手帮忙!
LoveCherry 2005-04-06
  • 打赏
  • 举报
回复
/*--获得汉字字符串的首字母--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fGetPy]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fGetPy]
GO

--创建取拼音函数
create function fGetPy(@Str varchar(500)='')
returns varchar(500)
as
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='a'
set @return=@return+@c
end
return(@return)
end

go
--测试
select dbo.fgetpy('魏保光') as 姓名拼音,dbo.fgetpy('ab中c国人') as 中国人
select dbo.fgetpy('刘子良') as 姓名拼音,dbo.fgetpy('ab中c国人') as 中国人
select dbo.fgetpy('吴过') as 姓名拼音,dbo.fgetpy('ab中c国人') as 中国人
select dbo.fgetpy('东北') as 姓名拼音,dbo.fgetpy('ab中c国人') as 中国人

--删除拼音函数
drop function fgetpy
safematch 2005-04-06
  • 打赏
  • 举报
回复
刚才找到一个函数,可以提取字符串的拼音首字母,可是怎么用到sql语句里呢
qing_zhou 2005-04-06
  • 打赏
  • 举报
回复
能说详细点么,我有点不明白你想做什么

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧