SQL按拼音字母查询指定字段的做法

摘要:
SQL根据拼音字母查询指定的字段。几天前,我们不得不根据拼音字母进行数据库查询。我们检查了很多信息,总结了一个更简单的方法,并与您分享。这种查询方法是以新华字典中某个元音的第一个汉字和下一个元音的首个汉字作为查询标准。
SQL按拼音字母查询指定字段的做法

   前段时间要做一个按拼音字母的数据库查询,查了好多资料,总结了一个比较简单一点的方法,拿出来和大家分享。
   比如说我们要查到指定字段第一个字是以韵母“L”开头的,我们可以用以下SQL语句进行查询:
   
SELECT * FROM 表名 WHERE author 所要查询的字段>='垃' AND 所要查询的字段 <'妈'
   
这个语句就能查询出“所要查询的字段”的第一个字以“L”开头的所要数据。
   这查询方法的依据是某一个韵母在新华字典中的首个汉字和下一个韵母的第一个汉字作为查询的条件,进行查询。如果查询的时候要包含英文字母,可以使用下面的语句:
   SELECT * FROM 表名 WHERE author 所要查询的字段 LIKE 'L'   OR ( 所要查询的字段>='垃' AND 所要查询的字段 <'妈')
   特别说明一下当要查询“Z”的时候,查询的条件是“ >='杂' OR  <'坐'  ”
   下面是韵母和汉字的对照表:

a: 吖b:巴c: 擦d: 搭e: 鹅 f: 发g: 旮
h: 哈i:j: 鸡 k: 喀l: 垃 m: 妈 n: 嗯
o: 哦p: 趴q: 欺  r: 然s: 仨t: 他
u:v:w: 挖 x: 西y: 压z: 杂

函数
SQL按拼音字母查询指定字段的做法第1张SQL按拼音字母查询指定字段的做法第2张Code
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_SearchWhere]'and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_SearchWhere]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_zhimu]'and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_zhimu]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE function f_SearchWhere(@Str nvarchar(200),@Col nvarchar(20))
returns  nvarchar(4000)
as
begin--函数实现开始

    
--====================================
    --zh 2008-08-12
    --@Str nvarchar(200)--查询条件内容
    --@Col nvarchar(20)--查询条件列
    --====================================

    
--自定变量
    declare @where nvarchar(4000)--返回查询条件变量
    declare @strLen int --传入的字串长度
    declare @i int--计数器
    declare @tmpstr  nvarchar(1)--字符缓存变量
    
    
--变量初始化
    set @strLen=LEN(@Str)
    
set @where=' where 1=1'
    
set @i=0
    
while(@i<@strLen)
    
begin
        
set @i=@i+1
        
set @tmpstr=SUBSTRING(@Str,@i,1)

        
if dbo.f_zhimu(@tmpstr)=1
        
begin
        
set @tmpstr=LOWER(@tmpstr)
        
if @tmpstr='a' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if @tmpstr='b' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='c' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='d' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='e' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='f' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='g' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='h' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
--else if  @tmpstr='i' 
            --set @where=@where+' or '+@Col+ ' LIKE '''+@tmpstr+'%'''
        else if  @tmpstr='j' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='k' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='l' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='m' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='n' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='o' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='p' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='q' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='r' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='s' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='t' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
--else if  @tmpstr='u' 
            --set @where=@where+' or '+@Col+ ' LIKE '''+@tmpstr+'%'''
        --else if  @tmpstr='v' 
            --set @where=@where+' or '+@Col+ ' LIKE '''+@tmpstr+'%'''
        else if  @tmpstr='w' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<''西'')'
        
else if  @tmpstr='x' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''西''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='y' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
else if  @tmpstr='z' 
            
set @where=@where+'  and  (SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)>=''''and SUBSTRING('+@Col+','+CAST(@i AS nvarchar)+',1)<'''')'
        
end
        
    
end
    
set @where=@where+' and '+@Col+ ' LIKE '''+@Str+'%'''

return(@where)
end

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE function f_zhimu(@Str nvarchar(1)='')

returns int
as
begin--函数实现开始
--
=====================
--
zh 2008-08-12
--
=====================
declare @tmpflag int
if UNICODE(@Str)>=97 and UNICODE(@Str)<=122
    
set @tmpflag=1
else
    
if UNICODE(@Str)>=65 and UNICODE(@Str)<=90
        
set @tmpflag=1
    
else
        
set @tmpflag=0
return(@tmpflag)
end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

过程

免责声明:文章转载自《SQL按拼音字母查询指定字段的做法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇WPF 获取DataGridRow有时为NULL的解决办法解决比较Oracle中CLOB字段问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java 字符串截取函数 substring()

在String中有两个substring()函数,如下: 一:String.substring(int start) 参数:     start:要截取位置的索引 返回:    从start开始到结束的字符串 例如:String str = "hello word!";         System.out.println(str.substring(1)...

身份证号码有效性检测算法 ( js版 转 C#版 )

C#版 #region 检测是否是正确的身份证 /// <summary> /// 身份证验证 /// </summary> /// <param name="num"></param> /// <returns></returns> public static bool isIdC...

JSP页面之${fn:}内置函数

函数列表: 函数名 函数说明 使用举例 fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}"> fn:containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:con...

FreeMarker操作符

操作字符串函数 1. substring(start,end)从一个字符串中截取子串 start:截取子串开始的索引,start必须大于等于0,小于等于end end: 截取子串的长度,end必须大于等于0,小于等于字符串长度,如果省略该参数,默认为字符串长度。 例子: ${‘str’?substring(0)} 结果为str ${‘str’?substr...

linux shell 学习笔记--变量声明与赋值,循环

Bash 变量是不分类型的 ------------------------ 不像其他程序语言一样,Bash 并不对变量区分"类型".本质上,Bash 变量都是字符串. 但是依赖于上下文,Bash 也允许比较操作和算术操作.决定这些的关键因素就是,变量中的值 是否只有数字. 赋值 a=123 a="sdsd" #注意=左右不要有空格,否则就变成比较字...

MySQL 常用字符串处理函数:截取、拼接、替换、正则表达式

一、字符串截取: 1. MySQL SUBSTRING 语法结构:SUBSTRING(str, pos),SUBSTRING(str FROM pos), SUBSTRING(str, pos, len) ,SUBSTRING(str FROM pos FOR len); 参数解释: 不带 len 参数的格式为返回一个从 pos 位置开始到str 末尾的...