sql Server获取表中今天、昨天、本周、上周、本月、上月等数据
2016-04-30 12:22
381 查看
在做Sql Server开发的时候有时需要获取表中今天、昨天、本周、上周、本月、上月等数据,这时候就需要使用DATEDIFF()函数及GetDate()函数了。
DATEDIFF ( datepart , startdate , enddate )
释义:计算时间差
datepare值:year | quarter | month | week | day | hour | minute | second | millisecond
startdate:开始日期
enddate :结束日期
GetDate()
释义:获取当前的系统日期
下面例子中表名为tablename,条件字段名为inputdate
查询今天
SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=0
查询昨天
SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=1
查询本周
SELECT * FROM tablename where datediff(week,inputdate,getdate())=0
查询上周
SELECT * FROM tablename where datediff(week,inputdate,getdate())=1
查询本月
SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=0
查询上月
SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=1
DATEDIFF ( datepart , startdate , enddate )
释义:计算时间差
datepare值:year | quarter | month | week | day | hour | minute | second | millisecond
startdate:开始日期
enddate :结束日期
GetDate()
释义:获取当前的系统日期
下面例子中表名为tablename,条件字段名为inputdate
查询今天
SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=0
查询昨天
SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=1
查询本周
SELECT * FROM tablename where datediff(week,inputdate,getdate())=0
查询上周
SELECT * FROM tablename where datediff(week,inputdate,getdate())=1
查询本月
SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=0
查询上月
SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=1
相关文章推荐
- Hadoop Hive sql语法详解
- C++通过OCCI操作Oracle数据库详解
- Hadoop Hive sql语法详解
- mysql主从复制(一)
- 数据分析≠Hadoop+NoSQL
- 数据分析≠Hadoop+NoSQL
- SQL解析过程详解
- 基于AST抽象语法树的SQL注入检测 (2) -- 每周小结(01-02~01-08) - .Little Hann
- OS X 上安装mysql
- yum mysql
- mysql locktables
- 阿里云虚拟主机建立wordpress站点时数据库创建失败问题
- mysql插入、更新与删除
- 批处理Batch,执行多个Sql语句
- 虚拟内存的使用--Redis学习笔记九
- MongoDB入门教程(二)
- Redis与Memcached的区别
- mysql中使用正则表达式查询
- mysql主从复制跳过错误
- 常用的 memcache