在sql server 2008中如何查询本日、本周和本月数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在sql server 2008中如何查询本日、本周和本月数据相关的知识,希望对你有一定的参考价值。
表名为table 列名为col。其中col这一列存的是写入数据时的日期,格式是datetime。请问查询当日、本周和本月内的数据的sql 语句怎么写?谢谢您了!
--本日select * from table where col between cast((convert(varchar(10),getdate(),120) as datetime) and cast((convert(varchar(10),getdate(),120)+\' 23:59:59.997\' as datetime)
或
SELECT * FROM table WHERE datediff(day,col,getdate())=0
--本周
SELECT * FROM table WHERE datediff(week,col,getdate())=0
--本月
select * from table where col between cast((convert(varchar(7),getdate(),120)+\'-1\' as datetime) and cast((convert(varchar(7),dateadd(m,1,,getdate()),120)+\'-1\' as datetime)来自:求助得到的回答 参考技术A 用到 datediff 函数: 返回跨两个指定日期和时间边界数。(获取两个时间之间的是时间段)
--今天
select * from table where datediff(day,col,getdate())=0
--本周
select * from table where datediff(week,col,getdate())=0
--本月
select * from table where datediff(month,col,getdate())=0
sql 在SQL Server上显示所有数据库
以上是关于在sql server 2008中如何查询本日、本周和本月数据的主要内容,如果未能解决你的问题,请参考以下文章