In SQL Server 2005 you can do date calculations in several ways. SQL Server 2005 has several functions you can use when you work with dates. This article describes a number of practical examples that you will run into when you work with databases and dates.

For example, it shows how to calculate back in time, how to calculate the difference in days between dates and how to get the month from a date.

DATEADD()

You use this function to find a date before or after a given date. For example, you want to know the date 2 months after today, or the date two days before 1 August.

Syntax DateAdd(datepart, number, date)Examples

The date of the day after tomorrow

T-SQL
SELECT DATEADD(dd, 2, GETDATE () )

In the example above we tell it to calculate in days (dd). The 2 tells it to count 2 days ahead. The third argument is the date to start from, in this case today (GETDATE() ). Instead of GETDATE() you can also pass a date, for example ‘2008-12-05’ . Note that it must be in quotes!

The date of two months ago

SQL
SELECT DATEADD(mm, -2, GETDATE () )

mm stands for months. -2 tells it to go back 2 months, and GETDATE() gives today’s date.

DATEPART()

With this function you can get the day, month, year and time from a date value. Say you want the month part of the date 2008-12-05, in this case 12. You may also need the day of 12 August 2007, which is 12.

Syntax
DatePart(datepart, date)

Examples

The day of today’s date

T-SQL
SELECT DATEPART(dd,GETDATE() )

The year of today’s date

T-SQL
SELECT DATEPART(yy,GETDATE() )

DATEDIFF()

You use Datediff to calculate an interval between two dates. That way you can find the number of days between two dates.

Syntax

DateDiff(datepart, date1, date2)

Examples

The number of days between 1 December 2008 and today (5 December 2008)

T-SQL
SELECT DATEDIFF(dd,'2008-12-01',GETDATE() )

DATENAME()

Datename is a lot like datepart. The difference is that this function returns a string (a piece of text), while datepart returns a number. This function can return the day of the week as text, or the name of the month.

Syntax

DateName(datepart, date)ExamplesThe name of today’s weekday

T-SQL
SELECT DATENAME (dw, GETDATE() )

Today’s day number in the year

T-SQL
SELECT DATENAME (dy, GETDATE() )

Below is a table with values you can enter as the first argument:

One limitation of the function is that it returns the English names. So I recommend that you always use a date dimension. The big advantage of a date dimension is that for every date you know which week it falls in, which year, which quarter, etc. Keep in mind that 1 January can fall in week 52 of the previous year. In that case the year of 1 January is, for example, 2008, but the year of the week number is 2007.

Frequently asked questions

How do I add days to a date in SQL Server?

Use DATEADD(datepart, number, date). DATEADD(dd, 2, GETDATE()) gives the date of the day after tomorrow. With a negative number you calculate back in time.

How do I calculate the number of days between two dates in SQL Server?

Use DATEDIFF(dd, date1, date2). DATEDIFF(dd,'2008-12-01',GETDATE()) gives the number of days between 1 December 2008 and today.

What is the difference between DATEPART and DATENAME?

DATEPART returns a number, DATENAME returns a piece of text. DATENAME can give, for example, the day of the week or the name of the month.

How do I get the month or the year from a date in SQL Server?

Use DATEPART with the datepart. DATEPART(yy,GETDATE()) gives the year of today's date and DATEPART(dd,GETDATE()) the day.

Why does DATENAME give English day names and what is the alternative?

DATENAME returns the English names. So use a date dimension instead. It holds the week, the year and the quarter for every date.

2 comments

Your email address is not shown. A photo comes from Gravatar, if you have one.

  1. Hallo,

    Ik ben op zoek naar een functie die het aantal werkdagen aangeeft tussen 2 datums. De workweek is van maandag tm vrijdag dus zaterdag en zondag tellen niet mee.

    Wie kan mij helpen?

  2. Deze functie kan je gebruiken maar dan heb je wel een datum tabel nodig:

    USE [DWH_NL_DMSA_1]
    GO

    /****** Object: UserDefinedFunction [dbo].[DateDiffWorkDays] Script Date: 10-2-2017 12:18:42 ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE FUNCTION [dbo].[DateDiffWorkDays]
    ( @StartDateID INT, @EndDateID INT, @IncludeHolidays BIT = 0 )

    ---------------------------------------------------
    -- Name : [dbo].[DateDiffWorkDays]
    -- Produced By : XXX
    -- URL : XXX
    -- Author : XXX
    -- Date : 6 Juni 2012
    -- Modified : XXX
    -- Modfied by : XXX
    -- Purpose : Calculate business days between two days.
    -- Called by : Different SQL
    -- Description :

    -- EXAMPLE :
    -- SELECT dbo.DateDiffWorkDays('2012-05-23','2012-05-31',0)
    ------------------------------------------------------------

    RETURNS INT

    AS

    BEGIN

    DECLARE @NrOfDays INT

    SELECT @NrOfDays = COUNT(*)
    FROM dbo.DimDatum AS DD
    WHERE DD.DatumID BETWEEN @StartDateID AND @EndDateID
    AND IsWerkDag= 1
    AND ( @IncludeHolidays = 1
    OR IsFeestDag_PS = 0)

    RETURN ( @NrOfDays )
    END

    GO