SQL SERVER – Find Weekend and Weekdays from Datetime in SQL Server 2012

Yesterday we had very first SQL Bangalore User Group meeting and I was asked following question right after the session.

“How do we know if today is a weekend or weekday using SQL Server Functions?”

Well, I assume most of us are using SQL Server 2012 so I will suggest following solution. I am using SQL Server 2012’s CHOOSE function. It is

SELECT GETDATE() Today,
DATENAME(dw, GETDATE()) DayofWeek,
CHOOSE(DATEPART(dw, GETDATE()), 'WEEKEND','Weekday',
'Weekday','Weekday','Weekday','Weekday','WEEKEND') WorkDay
GO

You can use the choose function on table as well. Here is the quick example of the same.

USE AdventureWorks2012
GO
SELECT A.ModifiedDate,
DATENAME(dw, A.ModifiedDate) DayofWeek,
CHOOSE(DATEPART(dw, A.ModifiedDate), 'WEEKEND','Weekday',
'Weekday','Weekday','Weekday','Weekday','WEEKEND') WorkDay
FROM [Person].[Address] A
GO

If you are using an earlier version of the SQL Server you can use a CASE statement instead of CHOOSE function.

Please read my earlier article which discusses CHOOSE function and CASE statements. Logical Function – CHOOSE() – A Quick Introduction

Reference:  Pinal Dave (http://blog.SQLAuthority.com)

10 thoughts on “SQL SERVER – Find Weekend and Weekdays from Datetime in SQL Server 2012

  1. This has a flaw in it in that it doesn’t consider the setting of @@DATEFIRST. In the US, they number Sunday as 1, Monday as 2 etc. Most other countries use Monday as 1, Tuesday as 2.

    This can be changed with the SET DATEFIRST command.

    Here’s my attempt, it will give consistent results whatever the setting of @@DATEFIRST is:

    SELECT today = datemaker.date,
    name = DATENAME(dw, datemaker.date),
    daynumber = 1 + ((DATEPART(dw, datemaker.date) + @@DATEFIRST – 2) % 7)
    FROM (
    — Make a list of dates starting from tomorrow
    SELECT TOP 21
    date = DATEADD(d, ROW_NUMBER() OVER (ORDER BY id), CONVERT(date, GETDATE()))
    FROM sys.sysobjects
    ) datemaker

    Like

  2. “I assume most of us are using SQL Server 2012…”

    Evidently you don’t guys work in the real world. Do you also assume that most of us are using Windows 8?

    Like

  3. Hi,

    I need quick help.

    I want to write a function which will take today’s date and reply me the number of weekdays only. For e.g. today is 04-28-2013. Result should be 20. How ?? today’s date minus 8 days as weekend passed. If today’s date is 0-17-2013 then result should be 12.

    Catch is to know how many weekends are passed from today’s date. I need to calculate dynamic everyday.

    Like

      • I just found this! Very usefull!

        DECLARE @StartDate DATETIME
        DECLARE @EndDate DATETIME
        SET @StartDate = ‘2008/10/01′
        SET @EndDate = ‘2008/10/31′

        SELECT
        (DATEDIFF(dd, @StartDate, @EndDate) + 1)
        -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
        -(CASE WHEN DATENAME(dw, @StartDate) = ‘Sunday’ THEN 1 ELSE 0 END)
        -(CASE WHEN DATENAME(dw, @EndDate) = ‘Saturday’ THEN 1 ELSE 0 END)

        Like

  4. To me, that code is *far* too dependent on specific date and language settings, aside from having extra overhead. How about instead:
    SELECT DATEDIFF(DAY, 0, A.ModifiedDate) % 7 <= 4 THEN 'Weekday' ELSE 'Weekend' END
    That code has *NO* reliance on date or language settings to determine the correct result.

    Like

  5. hi, can anyone tell me how to filter datetime column based on current date, weekly and monthly.
    Select * from timeattendance where clmdatetime = GETDATE() <== that is for datetime now, but I want the date without the time.
    How about filter weekly (monday to sunday) and monthly (ist of month and end of month)?

    Like

  6. Hi,
    My Table have week_end_date(Friday) and days of week as column, but you know some time weeks overlap in two months that causing wrong month wise report. Any suggestion how to consolidate data coreectly month wise.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s