Date Functions in SQL Server with Examples

21 Flares Filament.io 21 Flares ×

As a good programmer,you need to use many Date functions in SQL Server.I share here most used Date functions of SQL Server which will do different functionality according to its needs.

GETDATE()

You can easily retrieve current Date Time in SQL Server using GETDATE() function.

Query : select GETDATE()

CONVERT()

Many times,We want to convert Date Format in SQL Server.You can easily convert your Date format using CONVERT() function of SQL Server.

See below image.How Convert function works.

Date-Functios-in-SQL-Server

Above are few examples that we use.You can convert date in USA Date format like (MM-DD-YY) from 3rd query.

DATEPART()

DATEPART function of SQL server will give you day,month or year of Date.

You can get Day,Month & Year from your date of above 3 SQL Queries.

DATEDIFF()

DATEDIFF function of SQL Server returns time-interval between two dates.

Syntax: DATEDIFF(datepart,startdate,enddate)

You can use Day,Month,Year,DayOfYear,Quarter,Week,Hour etc. to get time period between two dates.

Query : SELECT DATEDIFF(Day,’2013-01-01′,’2013-03-24′) AS DiffDate will return 82.

DATEADD()

DATEADD function adds or subtracts a time interval from given date.

Syntax : DATEADD(Datepart,number,Date)

If we want to add 10 days from Today’s date,then DATEADD() function can be used as below.

Query : SELECT DATEADD(Day,10,GETDATE())

Related reading : 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">