SQL Server Date Functions

Date Functions

Date functions are used to manipulate datetime values (arithmetic operations) and parsing date components.

How to get current date ?

GETDATE() function is used to get the current date and time in SQL Server.

GETDATE Function

SELECT GETDATE()
Output:
2017-11-26 01:59:19.040

Datepart And Abbreviations

  • year : yy, yyyy
  • quarter : qq, q
  • month : mm ,m
  • day : dd, d (1 to 31)
  • day of year : dy, y (1 to 365)
  • week : wk, ww (0 to 51)
  • weekday : dw (1 to 7, here 1 is sunday, 7 is saturday)
  • hour : hh ( 0 to 23)
  • minute : mi, n ( 0 to 59 )
  • second : ss, s (0 to 59)
  • millisecond : ms (0 to 999)

DATEADD Function

Adds the number of date parts to the date.

SELECT DATEADD(year, 2, GETDATE())

Adds current date with 2 years.

SELECT DATEADD(year, 2, '2019-11-26 02:04:52.390')
Output:
2021-11-26 02:04:52.390

Abbreviation Datepart

Instead of year, we can also use either yy or yyyy abbreviations.

SELECT DATEADD(yy, 2, '2019-11-26 02:04:52.390')

DATEDIFF Function

DATEDIFF Function is used to calculate the date difference as date part between dates.

SELECT DATEDIFF(month, '2019-08-26 02:04:52.390', '2019-11-26 02:04:52.390')
Output:
3

returns number of month difference between dates '2019-08-26 02:04:52.390' and '2019-11-26 02:04:52.390'.

DATENAME Function

DATENAME function is used to return datepart as string in the specified date.

SELECT DATENAME(month, '2019-11-26 02:04:52.390')
Output:
November

DATEPART Function

DATEPART function is used to return datepart as integer in the specified date.

SELECT DATEPART(month, '2019-11-26 02:04:52.390')
Output:
11



Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^