当前位置:编程学习 > C#/ASP.NET >>

sql语句日期查询 。

有个表,表名为userlog,有一列为inputdate是日期变量,日期格式是2009-10-10 12:00:00 
如果我想查询这个表中2009年10月的内容,SQL语句该如何编写? 


就是知识查询这个月的数据。 --------------------编程问答-------------------- select * from userlog where inputdate  like '2009-10%' --------------------编程问答-------------------- select * from userlog where dataname(year,inputdate)=2009 and dataname(month,inputdate)=10 --------------------编程问答--------------------

select * from userlog where inputdate between '2009-10-01' and '2009-10-31'

日期类型可以直接这样使用 --------------------编程问答--------------------

select * from userlog where inputdate>=CONVERT(datetime,CONVERT(varchar(7),GETDATE(),111)+'-01')  and inputdate<CONVERT(datetime,CONVERT(varchar(7),Dateadd(MONTH,1,GETDATE()),111)+'-01')

只要把GETDATE()换成你要查询的时间就可以了。 --------------------编程问答-------------------- 方法不限一种,除了像楼上3楼的,还有如:
select * from userlog where convert(varchar(7),inputdate,23)='2009-10'
select * from userlog where datepart(yy,inputdate)='2009' and datepart(m,inputdate)='10'
...
--------------------编程问答-------------------- datediff(month,'2009-10-10','2009-10-01')=0 --------------------编程问答-------------------- select * from userlog where year(inputdate)='2009' and month(inputdate)='10' --------------------编程问答-------------------- 哎哟,不错哦! --------------------编程问答--------------------
引用 7 楼 work_fei 的回复:
select * from userlog where year(inputdate)='2009' and month(inputdate)='10'


+++
--------------------编程问答-------------------- select * from userlog where inputdate between '2009-10-01' and '2009-10-31'


--------------------编程问答--------------------
应该解决了吧
--------------------编程问答-------------------- 结帖率:0.00% 
    ....................................................... --------------------编程问答--------------------
引用 1 楼 naonaoye 的回复:
select * from userlog where inputdate like '2009-10%'

这个貌似不行   如果类型是varchar 还可以 要是Data的话就不行啦 --------------------编程问答-------------------- select * from userlog where convert(varchar(6),inputdate,112)='200910'
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,