T-SQL - Date Functions (2023)

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

Following is the list of date functions in MS SQL Server.

GETDATE()

It will return the current date along with time.

Syntax

Syntax for the above function −

GETDATE()

Example

The following query will return the current date along with time in MS SQL Server.

Select getdate() as currentdatetime

DATEPART()

It will return the part of date or time.

Syntax

Syntax for the above function −

DATEPART(datepart, datecolumnname)

Example

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

DATEADD()

It will display the date and time by add or subtract date and time interval.

Syntax

Syntax for the above function −

DATEADD(datepart, number, datecolumnname)

Example

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 

DATEDIFF()

It will display the date and time between two dates.

Syntax

Syntax for the above function −

DATEDIFF(datepart, startdate, enddate)

Example

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 

CONVERT()

It will display the date and time in different formats.

Syntax

Syntax for the above function −

CONVERT(datatype, expression, style)

Example

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)

Advertisements

';adpushup.triggerAd(ad_id);});

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated: 08/12/2023

Views: 6141

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.