随笔 - 833  文章 - 1  评论 - 106  阅读 - 200万

SQL查询今天、昨天、7天内、30天【转】

SQL查询今天、昨天、7天内、30天

今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0

昨天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=1

7天内的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=7

30天内的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=30

本月的所有数据:select * from 表名 where DateDiff(mm,datetime类型字段,getdate())=0

本年的所有数据:select * from 表名 where DateDiff(yy,datetime类型字段,getdate())=0

 

查询今天是今年的第几天: select datepart(dayofyear,getDate())

查询今天是本月的第几天:1. select datepart(dd, getDate())  

                                                2.select day(getDate())

查询本周的星期一日期是多少 (注意:指定日期不能是周日,如果是周日会计算到下周一去。所以如果是周日要减一天)
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)

 

查询昨天日期:select convert(char,dateadd(DD,-1,getdate()),111)  //111是样式号,(100-114)

 

查询本月第一天日期:Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) as firstday

查询本月最后一天日期:Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) as lastday      //修改-3的值会有相应的变化

 

本月有多少天:select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast((cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' ) as datetime ))))

 

求两个时间段相差几天:select datediff(day,'2012/8/1','2012/8/20') as daysum

在指定的日期上±N天:select convert(char,dateadd(dd,1,'2012/8/20'),111) as riqi    //输出2012/8/21

在指定的日期上±N分钟:select dateadd(mi,-15,getdate())  //查询当前时间15分钟之前的日期

 

http://www.cnblogs.com/XL-Tommy/p/6860174.html#3693298

posted on   3D入魔  阅读(828)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-12-01 Geometry Shader 【转】
2015-12-01 OpenGL - Tessellation Shader 【转】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示