DateTime functions in Sql Server

There are some DATE TIME functions in sql server and very useful to do in back end. The date and time is very easy to customize in the sql server.

GETDATE(): It returns the current date and time.

SELECT GETDATE()

DATEADD(): The DATEADD function used to add the day, month and year to the date time object.

SELECT DATEADD(DAY,13,GETDATE())
SELECT DATEADD(MONTH,3,GETDATE())
SELECT DATEADD(YEAR,13,GETDATE())

DATEPART(): The DATEPART is used to get the year, month, day, hour and minute from the data time object. It returns the integer value from the datetime object.

SELECT DATEPART(year,GETDATE())
SELECT DATEPART(month,GETDATE())
SELECT DATEPART(day,GETDATE())
SELECT DATEPART(hour,GETDATE())
SELECT DATEPART(minute,GETDATE())


DATEDIFF(): The DATEDIFF() function is used to calculate the day, month and year from the two dates.

The syntax is

DATEDIFF(DAY/MONTH/YEAR, start date, end date)

SELECT DATEDIFF(DAY,GETDATE()-5,GETDATE())
SELECT DATEDIFF(MONTH,GETDATE()-756,GETDATE())
SELECT DATEDIFF(YEAR,GETDATE()-4567,GETDATE())

DATETIME(): The DATETIME function is used to get the day, name of the month and year from the given datetime object.

SELECT DATENAME(DAY,GETDATE()) - Today's date in number
SELECT DATENAME(DW,GETDATE()) - Today's name
SELECT DATENAME(MONTH,GETDATE())
SELECT DATENAME(YEAR,GETDATE())


DAY(): It returns of day part from any date time object.

SELECT DAY(GETDATE())

MONTH(): It returns the month of the any date time object.

SELECT MONTH(GETDATE())

YEAR(): It returns the year of the any date time object

SELECT MONTH(GETDATE())

Ebook Download
View all
Learn
View all