I have a list of storecodes in table “Stores” and another table “StoreClosedDates” which tells me if the store is closed on a Saturday or Sunday. My StoreOpenDates table looks like this:
CREATE TABLE [dbo].[StoreClosedDates](
[StoreCode] [varchar](50) NOT NULL,
[ClosedOnSunday] [bit] NOT NULL,
[ClosedOnSaturday] [bit] NOT NULL
) ON [PRIMARY]
This table needs to be changed later to include holiday dates as well, so that those can be covered as well. I am not entirely surely how I can change this table to cover both options (holidays and weekends). Now I need to write a query which returns me a list of stores that are open for the current date. I am not sure how to compare for the weekend in the where clause – I know I should be using: DATEPART ( dw , getdate() ), but I cant seem to see the entire picture to solve it.
The StoreClosedDates contains only stores that closed. If a store is not present in that table, then the store is open for the current date.
Better avoid usage of
DATEPART, because it’s locale dependent.To check for the fixed date holidays, create a table with two separate columns containing the month and the date:
and use it in a query: