if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_GetPy]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_GetPy]
GO--创建取拼音函数
create function f_GetPy(@Str nvarchar(400))
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 @t 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 * from tabel_name  where  dbo.f_GetPy(name) like  'z%'

解决方案 »

  1.   

    太谢谢 Jaron(唐伯虎点蚊香不烧香)
    但我用的是Mysql数据库!郁闷呀!
      

  2.   

    楼上帖出的好像是sql server的
    mysql行不行呢?
      

  3.   

    答案是:肯定的.
    呵呵,让小弟来帮你解答吧,希望有用.看完问题翻历史资料找了半天才翻到的~~~_~如下做拼音码表:<?php
    /**
      拼音码表的生成:
          启动输入法生成器在逆转换选项卡中装入window的拼音输入法并将其保存为文
      本文件(winpy.txt)备用。
      
      下面的代码将拼音码表文件(文本)装入到数组$pymb中,结构为:(拼音,(汉字...))
      码表转换时间较长,应转换后另行保存
    */
    $filename = "pymb.txt";
    if(file_exists($filename)) {
      $fp = fopen($filename,"r");
      $pymb = unserialize(fread($fp,filesize($filename)));
      fclose($fp);
    }else {
      $filename = "winpy.txt";
      $fp = fopen($filename,"r");
      $old = "";
      $ar = array();
      $pymb = array();
      while(! feof($fp)) {
        $buffer = fgets($fp,128);
        sscanf($buffer,"%2s%s",$ch,$py);
        if($ch >= "啊" && ord($py) < 128) {
          $pymb[$ch] = $py;
        }
      }
      fclose($fp);
      $fp = fopen("pymb.txt","w");
      fwrite($fp,serialize($pymb)."\n");
      fclose($fp);
    }
    ?>
      应用例,给文字加上拼音<br>
    <?
    /**
      应用例,给文字加上拼音。为简化起见,假定全为中文
    */function get_py($text) {
      global $pymb;
      $i = 0;
      $n = strlen($text);
      $ar = array();
      while($i<$n) {
        $ch = $text[$i++];
        $py = " ";
        if(ord($ch) > 128) {
          $ch .= $text[$i++];
          $py = $pymb[$ch];
        }
        $ar[] = array($ch,$py);
      }
      return $ar;
    }$text = "  拼音码表的生成:
          启动输入法生成器在逆转换选项卡中装入window的拼音输入法并将其保存为文
      本文件(winpy.txt)备用。
      
      下面的代码将拼音码表文件(文本)装入到数组$pymb中,结构为:(拼音,(汉字...))
      码表转换时间较长,应转换后另行保存
    ";
    $ar = get_py($text);
    //print_r($ar);echo "<table><tr align=center>";
    for($i=0;$i<count($ar);$i++) {
      echo "<td>".$ar[$i][1]."</td>";
    }
    echo "</tr><tr align=center>";
    for($i=0;$i<count($ar);$i++) {
      echo "<td>".$ar[$i][0]."</td>";
    }
    echo "</tr></table>";
    ?>
      

  4.   

    Jophy(周逸)你好
    我按照你的方法运行后,显示的只有:
                                      
      拼 音 码 表 的 生 成 :         启 动 输 入 法 生 成 器 在 逆 转 换 选 项 卡 中 装 入 w i n d o w 的 拼 音 输 入 法 并 将 其 保 存 为 文     本 文 件 ( w i n p y . t x t ) 备 用 。         下 面 的 代 码 将 拼 音 码 表 文 件 ( 文 本 ) 装 入 到 数 组 ( 拼 音 , ( 汉 字 . . . ) )     码 表 转 换 时 间 较 长 , 应 转 换 后 另 行 保 存   
    而没有同时显示出拼音啊,不知道为什么?