SQL Clone
SQLServerCentral is supported by Redgate
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: 1368 | Views in the last 30 days: 2
Related Articles

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 “...


Un-SQL Friday 001: Branding

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


Monday Morning SQL Break – March 28, 2016

It’s Tuesday, and the Monday post is late.  A weekend of smoking quite a few pounds of pork kept me ...


Constant Data Types

Data types are an important part of how tables and variables work. Did you know that constants have ...

date manipulation