Following is the list of date functions in MS SQL Server.
It will return the current date along with time.
Syntax for the above function −
GETDATE()
The following query will return the current date along with time in MS SQL Server.
Select getdate() as currentdatetime
It will return the part of date or time.
Syntax for the above function −
DATEPART(datepart, datecolumnname)
Example 1 − The following query will return the part of current date in MS SQL Server.
Select datepart(day, getdate()) as currentdate
Example 2 − The following query will return the part of current month in MS SQL Server.
Select datepart(month, getdate()) as currentmonth
It will display the date and time by add or subtract date and time interval.
Syntax for the above function −
DATEADD(datepart, number, datecolumnname)
The following query will return the after 10 days date and time from the current date and time in MS SQL Server.
Select dateadd(day, 10, getdate()) as after10daysdatetimefromcurrentdatetime
It will display the date and time between two dates.
Syntax for the above function −
DATEDIFF(datepart, startdate, enddate)
The following query will return the difference of hours between 2015-11-16 and 2015-11-11 dates in MS SQL Server.
Select datediff(hour, 2015-11-16, 2015-11-11) as differencehoursbetween20151116and20151111
It will display the date and time in different formats.
Syntax for the above function −
CONVERT(datatype, expression, style)
The following queries will return the date and time in different format in MS SQL Server.
SELECT CONVERT(VARCHAR(19),GETDATE()) SELECT CONVERT(VARCHAR(10),GETDATE(),10) SELECT CONVERT(VARCHAR(10),GETDATE(),110)