2024年2月10日发(作者:)

SQL语句查询字段不包涵某个字符

sql中判断字符串中只包含或不包含某种字符的方法

通过2个函数charindex和patindex以及通配符的灵活使用可达目的。

charindex:查某字符(串)是否包含在其他字符串中,返回字符串中指定表达式的起始位置。

patindex:查某字符(串)是否包含在其他字符串中,返回指定表达式中某模式第一次出现的起始位置;如果在全部有效的文本和字符数据类型中没有找到该模式,则返回零。特殊:可以使用通配符!

例子:

1. 查询字符串中是否包含非数字字符

select patindex(%[^0-9]%, 1235x461)

select patindex(%[^0-9]%, 12350461)

2. 查询字符串中是否包含数字字符

select patindex(%[0-9]%, suyllgoo)

select patindex(%[0-9]%, suyllg0o)

3.函数判断字符串只包含数字

create function [dbo].fn_isnumeric

(

@pstring varchar(8000)

)

returns bit

with encryption

as

begin

declare @vjudge int

set @vjudge = 0

select @vjudge =

case

when patindex(%[0-9]%, lower(@pstring)) > 0 then 0

when patindex(%[0-9]%, lower(@pstring)) = 0 then 1

end

return @vjudge

end

4.函数判断字符串只包含字母(忽略大小写)

create function [dbo].fn_isalpha

(

@pstring varchar(8000)

)

returns bit

with encryption

as

begin

declare @vjudge int

set @vjudge = 0

select @vjudge =

case

when patindex(%[a-z]%, lower(@pstring)) > 0 then 0 when

patindex(%[a-z]%, lower(@pstring)) = 0 then 1 end

return @vjudge

end

5. 函数判断字符串不包含任何符号(包括空格)

create function [dbo].fn_isalphanumeric

(

@pstring varchar(8000)

)

returns bit

with encryption

as

begin

declare @vjudge int

set @vjudge = 0

select @vjudge =

case

when patindex(%[^a-z0-9]%, lower(@pstring)) > 0 then 0

when patindex(%[^a-z0-9]%, lower(@pstring)) = 0 then 1 end

return @vjudge

end

6. 函数判断字符串不包含任何符号(除空格外)

create function [dbo].fn_isalphanumericblank

(

@pstring varchar(8000)

)

returns bit

with encryption

as

begin

declare @vjudge int

set @vjudge = 0

select @vjudge =

case

when patindex(%[^a-z0-9 ]%, lower(@pstring)) > 0 then 0

when patindex(%[^a-z0-9 ]%, lower(@pstring)) = 0 then 1 end

return @vjudge

end

-- 注意:[^a-z0-9 ]模式中最后有一个空格。篇二:常用sql查询语句考试答案及解释 %代表任意多个字符 _代表一个字符

如果我就真的要查%或者_,怎么办呢?使用escape,转义字符后面的%或_就不作为通配符了,注意前面没有转义字符的%和_仍然起通

配符作用

select username from gg_user where username like %xiao_%

escape ;

select username from gg_user where username like %xiao%%

escape ;

a as b,就是给a起个别名叫b

select a.* from table_1 as a就是给table_1起个别名叫a,因此前面就可以使用a.*了

比如 name as 姓名这样的话,查询出来的列就是写姓名

一、单表查询练习

1、查询<学生信息表>,查询学生张三的全部基本信息

select *

from a_studentinfo

where sname=张三

2、查询<学生信息表>,查询学生张三和”李四”的基本信息

select *

from a_studentinfo

where sname=张三

or sname=李四

3、查询<学生信息表>,查询姓张学生的基本信息

select *

from a_studentinfo

where sname like 张%

4、查询<学生信息表>,查询姓名中含有四字的学生的基本信息

select *

from a_studentinfo

where sname like %四%

5、查询<学生信息表>,查询姓名长度为三个字,姓“李”,且最后一个字是“强”的全部学生信息。

select *

from a_studentinfo

where sname like 李_强

6、查询<学生信息表>,查询姓张或者姓”李”的学生的基本信息。

select *

from a_studentinfo

where sname like 张%

or sname like 李%

7、查询<学生信息表>,查询姓张并且所属省份是北京的学生信息

select *

from a_studentinfo

where sname like 张%

and province=北京

8、查询<学生信息表>,查询所属省份是北京、”新疆”、”山东”或者上海的学生的信息

select *

from a_studentinfo

where province in (北京,上海,新疆,山东)

9、查询<学生信息表>,查询姓张,但是所属省份不是北京的学生信息

select *

from a_studentinfo

where sname like 张%

and province !=北京

10、查询<学生信息表>,查询全部学生信息,并按照“性别”排序,性别相同的情况下按照“所属省份”排序,所属省份相同的情况下再按照“班级”排序

select *

from a_studentinfo

order by sex,province,class

11、查询<学生信息表>,查询现有学生都来自于哪些不同的省份

select distinct province as 省份

from a_studentinfo

12、查询<学生选修信息表>,查询没有填写成绩的学生的学号、课程号和成绩 select *

from a_studentcourse

where score is null

13、查询<学生选修信息表>,查询全部填写了成绩的学生的选修信息,并按照“成绩”从高到低进行排序 select *

from a_studentcourse

where score is not null

order by score desc

二、聚合函数练习

1、统计<学生信息表>,统计共有多少个学生

select count (*) as 学生数量

from a_studentinfo

2、统计<学生信息表>,统计年龄大于20岁的学生有多少个

select count(*) as 学生数量

from a_studentinfo

where (2008-yearofbirth)>20

3、统计<学生信息表>,统计入学时间在1980年至1982年的学生人数

select count(*) as 学生数量

from a_studentinfo

where eollment between 1998-01-01 and 2003-12-30

对比以下查询方式,看看有何不同,为什么?

select count(*) as 学生数量

from a_studentinfo

where eollment between 1998 and 2003

4、统计<学生选修信息表>,统计学号为s001的学生的平均成绩

select avg(score) as 平均成绩

from a_studentcourse

where sno=s001

5、统计<学生选修信息表>,统计学号为s001的学生的总成绩

select sum(score) as 总成绩

from a_studentcourse

where sno =s001

6、统计<学生选修信息表>,查询课程号为”c001”的课程的最高成绩

select max(score) as 最高成绩

from a_studentcourse

where cno=c001

7、统计<学生信息表>,查询所有学生中的最大年龄是多少

select 2008-min(yearofbirth) as 最大年龄

from a_studentinfo

三、分组查询练习

1、统计<学生选修信息表>,统计每个课程的选修人数

select cno,count(*) as 学生数量

from a_studentcourse

group by cno

2、统计<学生选修信息表>,统计每个同学的总成绩

select sno,sum(score) as 总成绩

from a_studentcourse

group by sno

3、统计<学生信息表>,统计每个班级中每种性别的学生人数,并按照班级排序 select class as 班级,sex as 性别, count(*) as 人数

from a_studentinfo

group by class,sex

order by class

4、统计<学生选修信息表>,统计每门课程的平均成绩,并按照成绩降序排序

select cno,avg(score) as 平均成绩

from a_studentcourse

group by cno

order by avg(score) desc

5、统计<学生选修信息表>,显示有两门以上课程不及格的学生的学号

select sno as 不及格学生学号

from a_studentcourse

where score<60

group by sno

having count(*)>1

(1. group by 语句简介:

group by语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(group)”。它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对若干个小区域进行数据处理。

2. group by 的使用:

2.1 group by [expressions]:

这个恐怕是group by语句最常见的用法了,group by + [分组字段](可以有多个)。在执行了这个操作以后,数据集将根据分组字段的值将一个数据集划分成各个不同的小组。比如有如下数据集,其中水果名称(fruitname)和出产国家(productplace)为联合主键:如果我们想知道每个国家有多少种水果,那么我们可以通过如下sql语句来完成:

select count(*) as水果种类,productplace as出产国

fromt_test_fruitinfo

group by productplace

这个sql语句就是使用了group by + 分组字段的方式,那么这句sql语句就可以解释成“我按照出产国家(productplace)将数据集进行分组,然后分别按照各个组来统计各自的记录数量。”

2.2 group by all [expressions] :

其中有这么一句话“如果使用all关键字,那么查询结果将包含由group by子句产生的所有组...没有all关键字,那么不显示不符合条件的行组。”这句话听起来好像挺耳熟的,对了,好像和left join 和

right join 有点像。其实这里是类比left join来进行理解的。还是基于如下这样一个数据集:

fruitname productplace price

apple china $1.1

apple japan $2.1

apple usa $2.5

orange china $0.8

banana china $3.1

peach usa $3.0

首先我们不使用带all关键字的group by语句:

selectcount(*)as水果种类,productplaceas出产国

fromt_test_fruitinfo

where(productplace<>japan)

groupbyproductplace

那么在最后结果中由于japan不符合where语句,所以分组结果中将不会出现japan。