Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

A Flexible Day Of Week Function

By Todd Young,

The "DayOfWeek" parameter included as part of the DatePart function
can be a handy tool for limiting data to specific days of the week but
it is not very friendly when the required days need to be passed as a
parameter to a stored procedure.

An alterative method can be used to provide pretty much unlimited
flexibility using an integer value and some constants.

First define your constants as such:

1 = Sunday
2 = Monday
4 = Tuesday
8 = Wednesday
16 = Thursday
32 = Friday
64 = Saturday

To get every Tuesday for the year, use the Tuesday variable.

exec sel_TestDates 4

To get get multiple days, just add the constants together.
If you wanted to just get the weekdays, add Monday through Friday
for a total of 62.

exec sel_TestDates 62

For just the weekends, add Saturday and Sunday.

exec sel_TestDates 65

You can use any combination of values for odd day groupings such as
Monday, Wednesday and Friday.

exec sel_TestDates 42

 

 



 

Total article views: 1315 | Views in the last 30 days: 5
 
Related Articles
BLOG

Un-SQL Friday 001: Branding

In the spirit of T-SQL Tuesday, I hereby declare this Friday Un-SQL Friday. While T-SQL Tuesday is “...

BLOG

Un-SQL Friday 001: Branding

Since it’s Sunday (almost Monday), I thought I would chip into Un-SQL Friday.  Before I get down...

BLOG

Motivational Monday - 8/30/10

Goor Morning folks !  It's time for another motivational Monday. Today I want to remind you to be c...

FORUM

Find number of weekdays(Monday,Tuesday..) between two dates yearwise

Hi All, I need to find average week day count on a yearly basis. My table has the creation date a...

Tags
date manipulation    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones