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. This question is about to Find Weekend and Weekdays from Datetime in SQL Server 2012.

“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

SQL SERVER - Find Weekend and Weekdays from Datetime in SQL Server 2012 getdatedisplay2

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

SQL SERVER - Find Weekend and Weekdays from Datetime in SQL Server 2012 getdatedisplay1

If you are using an earlier version of the SQL Server you can use a CASE statement instead of CHOOSE function. All system date and time values are derived from the operating system of the computer on which the instance of SQL Server is running.

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

Reference:  Pinal Dave (https://blog.sqlauthority.com)

SQL DateTime, SQL Function, SQL Scripts, SQL Server, SQL Server 2012
Previous Post
SQLAuthority News – Android Efficiency Tips and Tricks – Personal Technology Tip
Next Post
SQL SERVER – Sends backups to a Network Folder, FTP Server, Dropbox, Google Drive or Amazon S3

Related Posts

Leave a Reply