473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

T-Sql - Calculate Each Monday for a given time period. Help??!!

I am using SQL Server 2000. I need to query my database for all the
contracts that came in during a certain time frame (user is prompted
for reportingperiodid).

Table - Periods
Fields - Reporting Period id int
Reporting Period desc varchar(30)
Reporting Period Begin Date datetime
Reporting Period End Date datetime

If the user selects a 3 then the begin date is Jan. 1, 2004 and the
end date is June 30, 2004.

Now I need to calculate did any money come in for each week in that
time frame. I need to create a weekly list of all the weeks in that
time frame. Each time frame begins on a Monday. So my list would
look like
1/5/2004
1/12/2004
1/19/2004
1/26/2004

All the way to the end of that time period.

How do I create this weekly list from a given time period using T-SQL?
I would appreciate any and all help on this.

Thanks,

Tony
Jul 20 '05 #1
3 17897

"CrystalDBA" <tt******@hotmail.com> wrote in message
news:b0**************************@posting.google.c om...
I am using SQL Server 2000. I need to query my database for all the
contracts that came in during a certain time frame (user is prompted
for reportingperiodid).

Table - Periods
Fields - Reporting Period id int
Reporting Period desc varchar(30)
Reporting Period Begin Date datetime
Reporting Period End Date datetime

If the user selects a 3 then the begin date is Jan. 1, 2004 and the
end date is June 30, 2004.

Now I need to calculate did any money come in for each week in that
time frame. I need to create a weekly list of all the weeks in that
time frame. Each time frame begins on a Monday. So my list would
look like
1/5/2004
1/12/2004
1/19/2004
1/26/2004

All the way to the end of that time period.

How do I create this weekly list from a given time period using T-SQL?
I would appreciate any and all help on this.

Thanks,

Tony


If you're going to do a lot of this sort of thing, then you should probably
create a calendar table - it would make a lot of date-related queries much
easier. It's also useful for dealing with things like holidays, stock market
trading days etc. which vary widely from one place to another:

create table dbo.Calendar (
BaseDate datetime primary key,
DayOfWeek varchar(10) not null,
WeekOfYear int not null,
MonthOfYear varchar(10) not null,
Quarter int not null
/* Add any other useful columns */
)
go

declare @d datetime
set @d = '20040101'
while @d < '20050101'
begin
insert into dbo.Calendar values (
@d,
datename(dw, @d),
datepart(ww, @d),
datename(mm, @d),
datepart(qq, @d))
set @d = dateadd(dd, 1, @d)
end
go

select *
from dbo.Calendar
where DayOfWeek = 'Monday' and
BaseDate between '20040101' and '20040630'
go

Note that the week of the year may vary - see DATEPART in Books Online.

Simon
Jul 20 '05 #2
"CrystalDBA" <tt******@hotmail.com> wrote in message
news:b0**************************@posting.google.c om...
I am using SQL Server 2000. I need to query my database for all the
contracts that came in during a certain time frame (user is prompted
for reportingperiodid).

Table - Periods
Fields - Reporting Period id int
Reporting Period desc varchar(30)
Reporting Period Begin Date datetime
Reporting Period End Date datetime

If the user selects a 3 then the begin date is Jan. 1, 2004 and the
end date is June 30, 2004.

Now I need to calculate did any money come in for each week in that
time frame. I need to create a weekly list of all the weeks in that
time frame. Each time frame begins on a Monday. So my list would
look like
1/5/2004
1/12/2004
1/19/2004
1/26/2004

All the way to the end of that time period.

How do I create this weekly list from a given time period using T-SQL?
I would appreciate any and all help on this.

Thanks,

Tony


Here's a UDF that will get all dates, for a specific day of the week, within
a period. The day of the week is given by an integer where Sunday is 1,
Monday is 2,..., and Saturday is 7.

-- Helper views to enumerate nonnegative integers to some upper bound

CREATE VIEW Digits (d)
AS
SELECT 0
UNION ALL
SELECT 1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
UNION ALL
SELECT 7
UNION ALL
SELECT 8
UNION ALL
SELECT 9

CREATE VIEW NonnegativeIntegers (n)
AS
SELECT Ones.d + 10 * Tens.d + 100 * Hundreds.d
FROM Digits AS Ones
CROSS JOIN
Digits AS Tens
CROSS JOIN
Digits AS Hundreds

CREATE FUNCTION GetDatesInPeriod
(@start_date DATETIME,
@end_date DATETIME,
@day INT)
RETURNS TABLE
AS
RETURN(
SELECT @start_date +
(((@day - DATEPART(WEEKDAY, @start_date) + 7) % 7) +
(7 * I.n)) AS date_of_day,
I.n + 1 AS date_number,
@start_date AS start_period,
@end_date AS end_period
FROM NonnegativeIntegers AS I
WHERE @start_date +
(((@day - DATEPART(WEEKDAY, @start_date) + 7) % 7) +
(7 * I.n)) <= @end_date
)

-- Every Monday from 20040101 to 20040630
SELECT date_of_day, date_number, start_period, end_period
FROM GetDatesInPeriod('20040101', '20040630', 2)
ORDER BY date_number ASC

date_of_day date_number start_period end_period
2004-01-05 00:00:00.000 1 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-01-12 00:00:00.000 2 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-01-19 00:00:00.000 3 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-01-26 00:00:00.000 4 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-02-02 00:00:00.000 5 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-02-09 00:00:00.000 6 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-02-16 00:00:00.000 7 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-02-23 00:00:00.000 8 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-03-01 00:00:00.000 9 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-03-08 00:00:00.000 10 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-03-15 00:00:00.000 11 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-03-22 00:00:00.000 12 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-03-29 00:00:00.000 13 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-04-05 00:00:00.000 14 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-04-12 00:00:00.000 15 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-04-19 00:00:00.000 16 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-04-26 00:00:00.000 17 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-05-03 00:00:00.000 18 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-05-10 00:00:00.000 19 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-05-17 00:00:00.000 20 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-05-24 00:00:00.000 21 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-05-31 00:00:00.000 22 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-06-07 00:00:00.000 23 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-06-14 00:00:00.000 24 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-06-21 00:00:00.000 25 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000
2004-06-28 00:00:00.000 26 2004-01-01 00:00:00.000 2004-06-30 00:00:00.000

Regards,
jag
Jul 20 '05 #3
Here's a solution that's pretty simple:

CREATE FUNCTION [dbo].[udf_Dates] (@BeginDate datetime, @EndDate datetime,
@DOW int)
-- Where @DOW represents the integer value for the day of the week (Sunday =
1, Monday = 2, Tuesday = 3, etc)

RETURNS @Days TABLE(DOWDate datetime) AS

BEGIN

DECLARE @BDOW int

-- Determine what day of the week your begin date is
SELECT @BDOW = DATEPART(dw, @BeginDate)

-- If the current day of the week is not the desired day, advance the begin
date to that dow
IF @BDOW != @DOW SELECT @BeginDate = CASE WHEN @BDOW < @DOW THEN DATEADD(dd,
@DOW - @BDOW, @BeginDate) ELSE DATEADD(dd, @BDOW - @DOW, @BeginDate) END + 1

-- Then just loop until you get to your end date, adding seven days at a
time
WHILE @BeginDate <= @EndDate BEGIN
INSERT INTO @Days SELECT @BeginDate

SELECT @BeginDate = DATEADD(dd, 7, @BeginDate)
END

RETURN
END

Then just call :

select * from udf_Dates('1/1/2004', '6/30/2004', 2)

and you will get 28 dates from 1/5/2004 to 6/28/2004.

"CrystalDBA" <tt******@hotmail.com> wrote in message
news:b0**************************@posting.google.c om...
I am using SQL Server 2000. I need to query my database for all the
contracts that came in during a certain time frame (user is prompted
for reportingperiodid).

Table - Periods
Fields - Reporting Period id int
Reporting Period desc varchar(30)
Reporting Period Begin Date datetime
Reporting Period End Date datetime

If the user selects a 3 then the begin date is Jan. 1, 2004 and the
end date is June 30, 2004.

Now I need to calculate did any money come in for each week in that
time frame. I need to create a weekly list of all the weeks in that
time frame. Each time frame begins on a Monday. So my list would
look like
1/5/2004
1/12/2004
1/19/2004
1/26/2004

All the way to the end of that time period.

How do I create this weekly list from a given time period using T-SQL?
I would appreciate any and all help on this.

Thanks,

Tony

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave | last post by:
Hi I am hoping someone might be able to help me out with this. I am writing a helpdesk system which records agents logging in and out of the system. I need to write a stored procedure which...
5
by: Shahriar Shamil Uulu | last post by:
Hi All, i want to calculate the cpu time consumption and memory consuption of any program written in python during runtime on Linux Fedora Core - 2, P4, RAM-512 MB. Actually i have written one...
17
by: barkarlo | last post by:
I need help to make grand total time in continuous forms. to calculate total work time I use following formula =format(+1--nz();"short time"). but when I make grand total time in form footer (for...
14
meLady
by: meLady | last post by:
Hello, Here I am having a little bit confusion about how to calculate the total time of steps of a process (for example: registering a new user for a website) in MS Access: - step1: entering a...
1
by: richardson | last post by:
Hi all i need ur help on how to calculate the avg time of two time stamps like start_time and end_time . The ideal need is to know the average time taken by each job from a table . i tried ...
8
by: Magesh | last post by:
Consider a block, fncall( ); /* tells me the current millisecond or something like that: time-1 */ {/* block of code for which I need to know the exec time ... ... ... } fncall( ); /* tells...
1
by: dianatokatlidis | last post by:
Hello. I have 2 separate date fields in a query. One is called Effective_Date and the other is called Expiry_Date. I want to compare the time period between these 2 fields and select any records...
1
by: Charming12 | last post by:
Hi All, I am facing quite a unique problem here with DayLight saving time. I know Windows handles Daylight saving time internally to sync PC Clock . But now i need to pass the Information of...
1
by: sasasasa | last post by:
How can I loop through each date in a given time period so that I can add those dates as a column name in a table. I want to create datatable with the person's name and hours he worked in a given...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.