Useful SQL date processing snippets#

Here are some useful T-SQL code snippets that you can uses for manipulating date.

--calculates the first day of the month
select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

--calculates the first day of the year
select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

--calculates the first day of the quater
select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)

--calculates the first monday of the month
select DATEADD(wk, DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0)

--calculates the last day of the prior month
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))

--calculates the last day of the prior year
select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))

--calculates the last day of the current year
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate() )+1, 0))

--calculates the monday of the current week
select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)

--calculates the yesterdays date
select DATEADD(dd, DATEDIFF(dd,0,getdate()), -1)

SQL
Thursday, March 03, 2005 11:14:00 PM (GMT Standard Time, UTC+00:00) #    Comments [0]  |  Tracked by:
"re:Tutorial Galore!" (绝缘电阻测试仪) [Trackback]
"Cash Loans" (Cash Loans) [Trackback]
"Viagra" (Viagra) [Trackback]
"Health Insurance" (Health Insurance) [Trackback]
"Payday Loan" (Payday Loan) [Trackback]
"xenical" (xenical) [Trackback]

 

Comments are closed.
All content © 2010, Keith Rull
On this page
This site
Calendar
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.9074.18820

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: