Fork me on GitHub

模糊查询&&日期时间操作

一、模糊查询

1、采用“_”、“%”通配符进行查询

select * from Students where stu_name like '张_';--一个‘_’表示一个字符
select * from Students where stu_name like '张__'
select * from Students where stu_name like '_三';
select * from Students where stu_name like '张%';--%表示零个至多个字符
select * from Students where stu_name like '%三';

2、采用“[]”通配符进行查询

select stu_name from Students where stu_age like '1[0-9]';
select stu_name from Students where stu_age like '1[^7-9]';--不是7-9的
select stu_name from students where stu_name like '[^张]%';--查找不是姓张的学生的姓名

3、复杂模糊查询

select
学生姓名,年龄,性别,家庭住址
from
Students
where
学生姓名 like '张%'
and 年龄 like '2[2-7]'
and 家庭住址 like '河南%';

 二、时间日期操作

1、判断是否是日期IsDate函数

select 学生编号,学生姓名,
case when IsDate(出生年月)=1 then '是日期信息'
        else '不是日期信息'
end
as 生日字段是否是日期格式
from  Students;

2、将日期类型数据转换为字符串

select 出生年月,
Substring(Convert(varchar(20),出生年月),6,5)
as 月日
from Students;

3、查询指定时间段的数据

select stu_name from students where stu_birthday between '1990-4-19' and '1991-05-03';

4、按年、月、日查询数据 Year Month  Day

select 书号,书名,销售数量,日期 from tb_Book where Year(日期)='2010' and Month(日期)='08' and Day(日期)='26'

5、返回当前日期时间及对应的日期

select Getdate() as 当前日期时间,DATENAME(WEEKDAY,GETDATE()) as 星期;

6、查询指定时间间隔的数据

select 学生姓名,出生年月,Datediff(Year,出生年月,GetDate()) as 学生年龄 from tb_Student;

7、Not和谓词进行组合条件的查询

select * from Students where stu_math not Between 70 and 80;

8、Having子句:聚合不应出现在 WHERE 子句中,除非该聚合位于 HAVING 子句或选择列表所包含的子查询中,并且要对其进行聚合的列是外部引用。

select count(书号) as 记录条数,书号,书名,作者 from tb_Book Group By 书号,书名,作者 Having count(书号)>1;

 

 

 

 

posted @ 2014-05-11 22:42  种花生的读书人  阅读(2546)  评论(0编辑  收藏  举报

该博客仅作为记录笔记,转载随意