性能疯子 Blog

倚天照海花无数,流水山高心自知

SQL 汉字转为首字母大写字母,c#通过汉字获取其首字母大写

 

功能:根据返回所有汉字的首字母----

存储过程中: 调用:select [dbo].[fun_getPY](字段名) as zm from 表名 */ 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 UPPER(@PY) END GO


程序中:
#region 汉字获得其大写首字母方法。 ///<summary> /// 判断是否为汉字 ///</summary> ///<param name="chrStr">待检测字符串</param> ///<returns>是汉字返回true</returns> publicbool IsChineseCharacters(string chrStr) { Regex CheckStr =new Regex("[/u4e00-/u9fa5]"); return CheckStr.IsMatch(chrStr); } ///<summary> /// 得到每个汉字的字首拼音码字母(大写) ///</summary> ///<param name="chrStr">输入字符串</param> ///<returns>返回结果</returns> publicstring GetHeadCharacter(string chrStr) { string strHeadString =string.Empty; System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312"); for (int i =0; i < chrStr.Length; i++) { //检测该字符是否为汉字 //if (!IsChineseCharacters(chrStr.Substring(i, 1))) //{ // strHeadString += chrStr.Substring(i, 1); // continue; //} byte[] bytes = gb.GetBytes(chrStr.Substring(i, 1)); string lowCode = System.Convert.ToString(bytes[0] -0xA0, 16); string hightCode = System.Convert.ToString(bytes[1] -0xA0, 16); int nCode = Convert.ToUInt16(lowCode, 16) *100+ Convert.ToUInt16(hightCode, 16); //得到区位码 strHeadString += FirstLetter(nCode); } return strHeadString; } ///<summary> /// 通过汉字区位码得到其首字母(大写) ///</summary> ///<param name="nCode">汉字编码</param> ///<returns></returns> publicstring FirstLetter(int nCode) { if (nCode >=1601&& nCode <1637) return"A"; if (nCode >=1637&& nCode <1833) return"B"; if (nCode >=1833&& nCode <2078) return"C"; if (nCode >=2078&& nCode <2274) return"D"; if (nCode >=2274&& nCode <2302) return"E"; if (nCode >=2302&& nCode <2433) return"F"; if (nCode >=2433&& nCode <2594) return"G"; if (nCode >=2594&& nCode <2787) return"H"; if (nCode >=2787&& nCode <3106) return"J"; if (nCode >=3106&& nCode <3212) return"K"; if (nCode >=3212&& nCode <3472) return"L"; if (nCode >=3472&& nCode <3635) return"M"; if (nCode >=3635&& nCode <3722) return"N"; if (nCode >=3722&& nCode <3730) return"O"; if (nCode >=3730&& nCode <3858) return"P"; if (nCode >=3858&& nCode <4027) return"Q"; if (nCode >=4027&& nCode <4086) return"R"; if (nCode >=4086&& nCode <4390) return"S"; if (nCode >=4390&& nCode <4558) return"T"; if (nCode >=4558&& nCode <4684) return"W"; if (nCode >=4684&& nCode <4925) return"X"; if (nCode >=4925&& nCode <5249) return"Y"; if (nCode >=5249&& nCode <5590) return"Z"; return""; } #endregion

 

数据库中 将大写字母 转化为小写字母 可以使用 LOWER(‘stringtoswitch’);

仅提供代码,不要说自己不会调用奥。首次写博客i,希望大家支持。up!

posted @ 2011-07-17 10:56  性能疯子  阅读(2703)  评论(1编辑  收藏  举报
QQ联系我