一些sqlserver时间的操作

首先创建数据,类型可以为varchar类型,如图:

一些sqlserver时间的操作

创建一个表:

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'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值