您的位置:首页 > 数据库

一些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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: