惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

博客园 - Timothy Ye

扩展方法使用小结 IIS7中Host WCF遇到的问题 博客园字体美化小技巧 - Timothy Ye - 博客园 个人闲置物品挥泪处理(8月27日更新) Visual Studio IDE配色方案 HTC Touch Pro 的重力感应功能 C# IDE Mobile – Write your C# code anywhere! Visual Studio 2010 Beta1 is available for download 小黑降温记 Windows 7 新特性体验 Unity 学习笔记(3) -- 生命周期管理 换了新鼠标 Unity 学习笔记(2) -- 配置文件的使用 Unity 学习笔记(1) -- Unity简介及简单使用 Microsoft My Phone 体验 快速开启、屏蔽Vista下的UAC提示 Dynamic Plugins Manager (五) Plugins Manager 源码下载 Dynamic Plugins Manager (四) 插件及Demo源码下载 Dynamic Plugins Manager (三) Demo
SQL中的日期计算
Timothy Ye · 2009-02-22 · via 博客园 - Timothy Ye

这两天写一个和统计数据有关的存储过程,里面要利用日期进行一些计算和判断,也自然要利用SQL的一些日期相关的函数。这里略记一下,当是复习一下SQL。

利用SQL脚本内置的几个函数,我们能灵活的对日期进行计算和比较。常用的几个函数:GETDATE(),DATEDIFF(),DATEADD()

GETDATE() 当然顾名思义,得到当前的日期,返回类型是DateTime类型。

DATEDIFF ( datepart , startdate , enddate ) 用于判断在两个日期之间存在的指定时间间隔的数目。

第一个参数是指定时间间隔的类型,例如mm(月),dd(天),yy(年),ms(毫秒),ss(秒),不同的间隔类型,返回的结果也不一样。

DATEADD (datepart , number, date ) 用于日期运算的函数,将传入的日期,加上指定时间间隔数目的日期。

例如,计算得到本年的第一天:

Select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

我们来分析下这个SQL语句就可以知道,首先从最里面的getdate()开始,getdate()得到当前日期和时间,外层的datediff,计算当前日期和1900-01-01 00:00:00之间的时间间隔,返回单位是以年来统计的,如果我们分开看DATEDIFF(yy,0,getdate()),0)的结果,返回就是:109。返回的109,传递给最外层的DATEADD函数,将1900-01-01 00:00:00加上109年,得到的结果,自然就是2009-01-01 00:00:00了,也即本年的第一天。

同样,灵活的利用这几个函数的组合,我们可以得到不同的结果:

得到当月的第一天:Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

得到当前季度的第一天:Select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)

得到当天的起始时间: Select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

得到上个月最后一天: Select DATEADD(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))

得到上个月的第一天: Select DATEADD(m,-1,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))

其原理就是得到当月第一天,再减去三毫秒(SQL的时间以3毫秒为一个单位),这样以当前为2月,得到的结果就是:2009-01-31 23:59:59.997

得到去年的最后一天: Select DATEADD(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))

得到本月的最后一天: Select DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))

得到本年的最后一天: Select DATEADD(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))

得到本月的第一个星期一: Select DATEADD(wk, DATEDIFF(wk,0,DATEADD(dd,6-datepart(day,getdate()),getdate())), 0)