Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Working days calendar in T-SQL?

431 views
Skip to first unread message

pedestrian via SQLMonster.com

unread,
Jun 19, 2007, 9:22:22 AM6/19/07
to
My company working days is from Monday to Friday.
I would like to generate a result set which show all
consecutive working days (Monday to Friday excluding the
weekends) that is, for June 2007, it show me 1st, 4th, 5th,
6th, 8th, 11th etc... until 29th (last working day of June). Is
it possible to do this with T-SQL?

Thank you very much.

--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/200706/1

Alex Kuznetsov

unread,
Jun 19, 2007, 9:29:32 AM6/19/07
to
On Jun 19, 8:22 am, "pedestrian via SQLMonster.com" <u16758@uwe>
wrote:
> Message posted via SQLMonster.comhttp://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/2007...

Yes, google up "calendar table".

Uri Dimant

unread,
Jun 19, 2007, 9:27:47 AM6/19/07
to
Hi
http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-count-the-number-of-business-days-between-two-dates.html


"pedestrian via SQLMonster.com" <u16758@uwe> wrote in message
news:73eef10337f0a@uwe...

David Portas

unread,
Jun 19, 2007, 9:36:26 AM6/19/07
to
On 19 Jun, 14:22, "pedestrian via SQLMonster.com" <u16758@uwe> wrote:
> My company working days is from Monday to Friday.

Is that always the case though? What about public holidays?


> I would like to generate a result set which show all
> consecutive working days (Monday to Friday excluding the
> weekends) that is, for June 2007, it show me 1st, 4th, 5th,
> 6th, 8th, 11th etc... until 29th (last working day of June). Is
> it possible to do this with T-SQL?

Use a calendar table:

SELECT dt
FROM Calendar
WHERE dt > '20070601'
AND dt < '20070701'
AND WorkingDay = 'Y';

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

ML

unread,
Jun 19, 2007, 9:34:16 AM6/19/07
to

pedestrian via SQLMonster.com

unread,
Jun 20, 2007, 8:47:10 AM6/20/07
to
Hi, Uri, thanks for the link for useful information...

Uri Dimant wrote:
>Hi
>http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-count-the-number-of-business-days-between-two-dates.html


>
>> My company working days is from Monday to Friday.
>> I would like to generate a result set which show all

>[quoted text clipped - 4 lines]
>>
>> Thank you very much.

--
Regards,
Pedestrian, Penang.

Message posted via http://www.sqlmonster.com

pedestrian via SQLMonster.com

unread,
Jun 20, 2007, 8:46:50 AM6/20/07
to
Hi, Uri, thanks for the link for useful information...

>> My company working days is from Monday to Friday.
>> I would like to generate a result set which show all

>[quoted text clipped - 4 lines]
>>

>> Thank you very much.

--
Regards,
Pedestrian, Penang.

pedestrian via SQLMonster.com

unread,
Jun 20, 2007, 8:48:11 AM6/20/07
to
Thanks for the suggestion, ML.
Thank you Alex.

--
Regards,
Pedestrian, Penang.

Message posted via http://www.sqlmonster.com

pedestrian via SQLMonster.com

unread,
Jun 20, 2007, 8:54:42 AM6/20/07
to
Ya.... I have to take care the holiday as well (which is stored in a table)
FYI, I have decided to create an Windows app that will be scheduled daily...

--
Regards,
Pedestrian, Penang.

--CELKO--

unread,
Jun 20, 2007, 12:27:49 PM6/20/07
to
Build a calendar table with one column for the calendar data and other
columns to show whatever your business needs in the way of temporal
information. Do not try to calculate holidays in SQL -- Easter alone
requires too much math.

CREATE TABLE Calendar
(cal_date DATE NOT NULL PRIMARY KEY,
fiscal_year INTEGER NOT NULL,
fiscal_month INTEGER NOT NULL,
week_in_year INTEGER NOT NULL, -- SQL server is not ISO standard
holiday_type INTEGER NOT NULL
CHECK(holiday_type IN ( ..), --
day_in_year INTEGER NOT NULL,
julian_business_day INTEGER NOT NULL,
...);

The Julian business day is a good trick. Number the days from
whenever your calendar starts and repeat a number for a weekend or
company holiday.

A calendar table for US Secular holidays can be built from the data at
this website, so you will get the three-day weekends:

http://www.smart.net/~mmontes/ushols.html

Time zones with fractional hour displacements
http://www.timeanddate.com/worldclock/city.html?n=246
http://www.timeanddate.com/worldclock/city.html?n=176
http://www.timeanddate.com/worldclock/city.html?n=5
http://www.timeanddate.com/worldclock/city.html?n=54

But the STRANGE ones are:
http://www.timeanddate.com/worldclock/city.html?n=63
http://www.timeanddate.com/worldclock/city.html?n=5

raibeart

unread,
Jun 20, 2007, 1:59:48 PM6/20/07
to
I use a concept that data warehouses use, a dimension table.

CREATE TABLE [dbo].[TIME_DIMENSION](
[DATE_ID] [datetime] NOT NULL,
[DAY_OF_WEEK] [varchar](50) NULL,
[DAY_MONTH_NO] [int] NULL,
[DAY_MONTH_TXT] [varchar](12) NULL,
[DAY_YEAR_NO] [smallint] NULL,
[DAY_OVERALL_NO] [int] NULL,
[WEEK_YEAR_NO] [int] NULL,
[WEEK_OVERALL_NO] [int] NULL,
[MONTH_NO] [int] NULL,
[MONTH_TXT] [varchar](15) NULL,
[MONTH_OVERALL_NO] [int] NULL,
[QUARTER_NO] [int] NULL,
[FISCAL_PERIOD] [varchar](50) NULL,
[HOLIDAY_FLG] [varchar](50) NULL,
[WEEKDAY_FLG] [varchar](50) NULL,
[LAST_DAY_IN_MONTH_FLG] [varchar](50) NULL,
[YEAR_NO] [smallint] NULL,
[CREATION_DATE] [datetime] NULL,
[APP_LOGIN_CREATION] [varchar](50) NULL,
[MODIFICATION_DATE] [datetime] NULL,
[APP_LOGIN_MODIFICATION] [varchar](50) NULL,
CONSTRAINT [TIME_DIMENSION_PK] PRIMARY KEY CLUSTERED
(
[DATE_ID] ASC
)ON [PRIMARY]
) ON [PRIMARY]


Using this, there is essentially no math that needs to be done on
dates. You
simply join this tablewith any date column and you get all the
information about it.

I use an Access program to load the table initially and usually carry
100 to
150 years of data in it.

Robert


On Jun 19, 8:22 am, "pedestrian via SQLMonster.com" <u16758@uwe>
wrote:

> Message posted via SQLMonster.comhttp://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/2007...


pedestrian via SQLMonster.com

unread,
Jun 21, 2007, 9:37:43 AM6/21/07
to
To both CELKO and raibeart,
Thank you guy for the suggested solution...

>[quoted text clipped - 7 lines]


>> --
>> Message posted via SQLMonster.comhttp://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/2007...

--
Regards,
Pedestrian, Penang.

0 new messages