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' --------------------编程问答-------------------- 哎哟,不错哦! --------------------编程问答--------------------
+++
--------------------编程问答-------------------- select * from userlog where inputdate between '2009-10-01' and '2009-10-31'
--------------------编程问答--------------------
应该解决了吧
--------------------编程问答-------------------- 结帖率:0.00%
....................................................... --------------------编程问答--------------------
这个貌似不行 如果类型是varchar 还可以 要是Data的话就不行啦 --------------------编程问答-------------------- select * from userlog where convert(varchar(6),inputdate,112)='200910'
补充:.NET技术 , ASP.NET