SQL SERVER – Few Useful DateTime Functions to Find Specific Dates

Recently I have recieved email from

----Today
SELECT GETDATE() 'Today'
----Yesterday
SELECT DATEADD(d,-1,GETDATE()) 'Yesterday'
----First Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) 'First Day of Current Week'
----Last Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) 'Last Day of Current Week'
----First Day of Last Week
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) 'First Day of Last Week'
----Last Day of Last Week
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6) 'Last Day of Last Week'
----First Day of Current Month
SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) 'First Day of Current Month'
----Last Day of Current Month
SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))) 'Last Day of Current Month'
----First Day of Last Month
SELECT DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) 'First Day of Last Month'
----Last Day of Last Month
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) 'Last Day of Last Month'
----First Day of Current Year
SELECT DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0) 'First Day of Current Year'
----Last Day of Current Year
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0))) 'Last Day of Current Year'
----First Day of Last Year
SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)) 'First Day of Last Year'
----Last Day of Last Year
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))) 'Last Day of Last Year'

ResultSet:

Today
———————–
2008-08-29 21:54:58.967

Yesterday
———————–
2008-08-28 21:54:58.967

First Day of Current Week
————————-
2008-08-25 00:00:00.000

Last Day of Current Week
————————
2008-08-31 00:00:00.000

First Day of Last Week
———————–
2008-08-18 00:00:00.000

Last Day of Last Week
———————–
2008-08-24 00:00:00.000

First Day of Current Month
————————–
2008-08-01 00:00:00.000

Last Day of Current Month
————————-
2008-08-31 23:59:59.997

First Day of Last Month
———————–
2008-07-01 00:00:00.000

Last Day of Last Month
———————–
2008-07-31 23:59:59.997

First Day of Current Year
————————-
2008-01-01 00:00:00.000

Last Day of Current Year
————————
2008-12-31 23:59:59.997

First Day of Last Year
———————–
2007-01-01 00:00:00.000

Last Day of Last Year
———————–
2007-12-31 23:59:59.997

Reference : Pinal Dave (https://blog.sqlauthority.com),

SQL DateTime, SQL Function, SQL Scripts
Previous Post
SQL SERVER – 2008 – Introduction to Merge Statement – One Statement for INSERT, UPDATE, DELETE
Next Post
SQL SERVER – FIX : ERROR : Could Not Connect to SQL Server – TDSSNIClient initialization failed with error 0x7e, status code 0x60

Related Posts

112 Comments. Leave new

  • Marek ÅšliwiÅ„ski
    August 30, 2008 3:36 am

    thx! @ll in 1 place :) Great! fav+

    Reply
  • –First Day Of Current Quarter
    SELECT Dateadd(qq, Datediff(qq,0,GetDate()), 0)

    –Last Day Of Current Quarter
    SELECT Dateadd(ms,-3,Dateadd(qq, Datediff(qq,0,GetDate())+1, 0))

    –First Day Of Prior Quarter
    SELECT Dateadd(qq, Datediff(qq,0,GetDate())-1, 0)

    –Last Day Of Prior Quarter
    SELECT Dateadd(ms,-3,Dateadd(qq, Datediff(qq,0,GetDate()), 0))

    Reply
  • Hi,
    The above informations are really very fantastic..
    May i know the how to down load sqlserver 2008 book.

    Reply
  • Great info indeed (and your blog in general is full of ressources).
    For some reporting, I need the first and last days of previous month (those I got from your useful dateTime functions) but also the first and last days of 2 months ago as well as the first and last days of 3 months… what ever is the day of today (getdate()). Any hint?

    Reply
  • Rama Krishna.K.S.
    September 16, 2008 3:31 pm

    This collection will be useful for everybody, wonderful

    Reply
  • Hey.., information was sufficient enough…, thanks and good bye

    Reply
  • When it comes to processing dates, one thing I failed to see online is the calculation for Retail Calendar. I’m in the Retail industry and calendar is different from the Gregorian calendar. Any clue in developing a Retail Calendar other then creating a custom calendar table?
    Thanks,
    Chris

    Reply
  • what if you want results from a specific date? for example, the 3rd of every month?

    Reply
  • Great Information .Thank You

    Reply
  • it s most usefull for developer

    Reply
  • Thanks so much! A real life saver….well, at the very least, a huge time and mental anguish saver.

    Reply
  • Very useful functions no doubt , real life saver Thanks.

    Reply
  • Very useful indeed. Thanks.

    Reply
  • Hi Dave,

    Thank you for all you have given, all your efforts, all your sharing and goodness to so many.

    I pray great blessings will come your way for all those you have helped.

    RSJ

    Reply
  • i want to know the query of the month difference between two date as specific month name in sql server 2005! r u help me .

    Reply
  • Hi Rajnish,

    You can use:

    DATEDIFF(mm, Date1, Date2) to get month difference between Dates.

    Thanks,

    Tejas

    Reply
  • I\’d like to know how to get three months before today so that it changes every day that I open up MS SQL Server 2005. Does anyone know?

    Reply
  • It’s ok I’ve found out now for myself. Gotta love S Q L!

    Reply
  • Hi Dave,
    Thanq very much for the above information.I appreciate your efforts

    Reply
  • Great Tips,

    Reply

Leave a Reply