[quoted text, click to view] On Mon, 27 Jun 2005 23:51:01 -0700, Tango wrote:
>Hi
>can i please have some assistance around performing a sql query that inserts
>data based on a date range.
>
>for example my initial query is
>INSERT INTO tbl_master
> ([date], unavailableemployeeid, reasonid,
>unavailablehours)
>VALUES (, 1234, 12, 7)
>
>but i want to insert a record for say 01/06/05 - 30/06/05.
>
>Id also like to be able to not add a record for sundays.
>
>Thankyou in advance
>
>Todd
Hi Todd,
A calendar table is an invaluable tool for tasks like this. Check out
http://www.aspfaq.com/show.asp?id=2519 for lots of information,
including the code to make one and some examples of how to use it. Note
that you only need to create a calendar table once, but you can use it
as often as you want!
For the task at hand, use
INSERT INTO tbl_master
([date], unavailableemployeeid, reasonid, unavailablehours)
SELECT dt, 1234, 12, 7
FROM dbo.Calendar
WHERE dt >= '20050601' -- Note the standard date format yyyymmdd
AND dt < '20050701' -- Note how I test for end of date range
AND dayname <> 'Sunday'
Best, Hugo
--