一些sqlserver时间的操作
2012-11-24 08:33
204 查看
首先创建数据,类型可以为varchar类型,如图:
创建一个表:
create table Material(
mid int identity(1,1),
createTime varchar(20),
primary key (mid)
)
可以进行数据的操作,请看以下:
select * from Material ;
insert into Material (createTime) values(convert(varchar(10),getdate(),120));
1.年的查询
--查询某年 比如查询2011年的数据
select * from Material where year(createTime)=2011
--查询某年到某年 比如查询2011年到2012年
select * from Material where year(createTime) between 2011 and 2013
2.月的查询
--查询某月 比如查询2011年11月份数据
select *from Material where convert(varchar(7),createTime,120)= '2011-11'
select * from Material where year(createTime)=2011 and month(createTime)=10
select * from Material where createTime between datename(year,getdate())+'-10-01' and datename(year,getdate())+'-10-30'
--查询某年某月 到 某年某月 比如查询2011年10月到2011年11月的之间的数据()
select * from Material where convert(varchar(7),createTime,120) between '2011-09' and '2011-10'
3.日的查询
--查询某日 比如查询2011年10月7日的数据
select * from Material where year(createTime)=2011 and month(createTime)=10 and day(createTime)=7;
--查询某年某月某日 到 某年某月某日
select * from Material where createTime between '2011-10-1' and '2011-11-6'
select * from Material where createTime between datename(year,getdate())+'-10-1' and datename(year,getdate())+'-11-8'
创建一个表:
create table Material(
mid int identity(1,1),
createTime varchar(20),
primary key (mid)
)
可以进行数据的操作,请看以下:
select * from Material ;
insert into Material (createTime) values(convert(varchar(10),getdate(),120));
1.年的查询
--查询某年 比如查询2011年的数据
select * from Material where year(createTime)=2011
--查询某年到某年 比如查询2011年到2012年
select * from Material where year(createTime) between 2011 and 2013
2.月的查询
--查询某月 比如查询2011年11月份数据
select *from Material where convert(varchar(7),createTime,120)= '2011-11'
select * from Material where year(createTime)=2011 and month(createTime)=10
select * from Material where createTime between datename(year,getdate())+'-10-01' and datename(year,getdate())+'-10-30'
--查询某年某月 到 某年某月 比如查询2011年10月到2011年11月的之间的数据()
select * from Material where convert(varchar(7),createTime,120) between '2011-09' and '2011-10'
3.日的查询
--查询某日 比如查询2011年10月7日的数据
select * from Material where year(createTime)=2011 and month(createTime)=10 and day(createTime)=7;
--查询某年某月某日 到 某年某月某日
select * from Material where createTime between '2011-10-1' and '2011-11-6'
select * from Material where createTime between datename(year,getdate())+'-10-1' and datename(year,getdate())+'-11-8'
相关文章推荐
- Java中Calendar的一些简单操作 及 JS中获取当前时间前一天时间(js中时间Date操作)
- php 操作 sqlserver 数据库 关于时间的小小见解
- 对时间的一些操作总结
- 解决SQLServer中变更海量数据表结构时产生【无法修改表. Timeout 时间已到. 在操作完成之前超时时间已过或服务器未响应. 】
- 浅谈Python中用datetime包进行对时间的一些操作
- Js获取当前日期时间及其它操作(一些时间操作)
- Calendar类的一些常用的时间操作
- java中获取时间的一些常用操作
- php操作sqlserver关于时间日期读取的小小见解
- Android root环境下的一些可用操作(关机命令,系统时间,重启命令)
- sqlserver知识点汇总(这段时间复习和回顾了一下sqlserver的一些知识),供大家参考和学习!
- 浅谈Python中用datetime包进行对时间的一些操作
- oracle数据库的一些时间加减操作
- JAVA一些常用的时间操作
- 关于java时间与日期的一些操作
- 针对时间轴TimeLine的一些js操作
- 整理的一些常用的Java日期时间上面的操作
- python 有关时间操作的一些代码
- sqlserver中对时间日期的操作
- 关于数据库的一些操作! 实用为主! 有关于时间加减!临时表等……