473,765 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 reportingperiod id).

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 17915

"CrystalDBA " <tt******@hotma il.com> wrote in message
news:b0******** *************** ***@posting.goo gle.com...
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 reportingperiod id).

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******@hotma il.com> wrote in message
news:b0******** *************** ***@posting.goo gle.com...
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 reportingperiod id).

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 NonnegativeInte gers (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 GetDatesInPerio d
(@start_date DATETIME,
@end_date DATETIME,
@day INT)
RETURNS TABLE
AS
RETURN(
SELECT @start_date +
(((@day - DATEPART(WEEKDA Y, @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 NonnegativeInte gers AS I
WHERE @start_date +
(((@day - DATEPART(WEEKDA Y, @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 GetDatesInPerio d('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******@hotma il.com> wrote in message
news:b0******** *************** ***@posting.goo gle.com...
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 reportingperiod id).

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
2700
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 will show the peak number of agents logged in concurrently during a specified time period. Within the time period, the person viewing the report should be able to
5
2807
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 database program in two ways, 1) Inprocedural way 2) in object oriented way. Now i want to check how efficient each function, object,method and variable is. How i can do this ? Any help will be greatly apperciated ... Thank you ...
17
11230
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 all records) my result is error. My grand total must be in format . thanks,
14
9903
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 user name in a text box will take 00:00:10,94 - step2: entering a user password in a text box will take 00:00:03,12 - step3: entering a user confirmation password will take 00:01:30,16 - step4: entering a user email address in a text box will take...
1
4006
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 SELECT job_name,AVG(end_time - start_time) FROM rdbdev.loop_through_job_stats WHERE report_id =12059909 GROUP BY job_name the avg returned returns in an exponentiation values like 9.25925925925926E-7
8
3777
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 me the current millisecond or something like that:
1
1433
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 that fall in the month of January-2008. For example if the Effective_Date = Dec 1, 2007 and Expiry_Date = Feb 5, 2008, then I would keep this record as January-2008 would fall within this time period. If the Effective_Date=Nov 1, 2007 and Expiry...
1
1707
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 Daylight Saving period i.e, From When To When it is applied . For e.g. In US it is From 2nd Sunday in March to 1st Sunday in November. I need to get this period for all timeZones in my .Net application using Windows API.
1
7413
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 time period. The user will select the dates and I want to show each day/date in that table as a column name. I have no idea how to do it. Please help.
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9833
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.