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

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
V
V2EX
S
Security Affairs
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
J
Java Code Geeks
The Register - Security
The Register - Security
U
Unit 42
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Project Zero
Project Zero
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
博客园 - 司徒正美
V
Vulnerabilities – Threatpost
T
Tor Project blog
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Palo Alto Networks Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta

博客园 - 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)