BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL to return a row for each day in a date range

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Matthew Pascoe
Guest





PostPosted: Sun Oct 17, 2004 9:04 am    Post subject: SQL to return a row for each day in a date range Reply with quote



would like to write an SQL statement that will return a row for each day
that is in a date range that I provide.

For example if the date range is 01-10-04 to 04-10-04 I would like the
following result set:

Day(DateTime data type)
01-10-04
02-10-04
03-10-04
04-10-04


I need to use this as the base for creating a more complex query.

Thanks for any help.

Matthew


Back to top
Alain Quesnel
Guest





PostPosted: Sun Oct 17, 2004 1:04 pm    Post subject: Re: SQL to return a row for each day in a date range Reply with quote



You could do that within a stored proc, or create a UDF that returns a table
type.

What RDBMS are you using?

--

Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Matthew Pascoe" <mpascoe (AT) stocklogix (DOT) com.au> wrote

Quote:
would like to write an SQL statement that will return a row for each day
that is in a date range that I provide.

For example if the date range is 01-10-04 to 04-10-04 I would like the
following result set:

Day(DateTime data type)
01-10-04
02-10-04
03-10-04
04-10-04


I need to use this as the base for creating a more complex query.

Thanks for any help.

Matthew





Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Sun Oct 17, 2004 3:08 pm    Post subject: Re: SQL to return a row for each day in a date range Reply with quote



Matthew Pascoe wrote:
Quote:
would like to write an SQL statement that will return a row for each
day that is in a date range that I provide.

For example if the date range is 01-10-04 to 04-10-04 I would like the
following result set:

Day(DateTime data type)
01-10-04
02-10-04
03-10-04
04-10-04

Not possible to manufacture data that doesn't exist with SQL. You can do
this with a stored procedure, or if your database does not support stored
procedures, then you could also do it by creating a table that has all dates
for whatever periods you need to query, then SQL can select dates in a
specified range from that and join against other tables as necessary.

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"Nature abhors the vacuum tube." - J.R. Pierce, Bell Labs engineer who
coined the term 'transistor'



Back to top
Matthew Pascoe
Guest





PostPosted: Sun Oct 17, 2004 11:24 pm    Post subject: Re: SQL to return a row for each day in a date range Reply with quote

SQL Server 2000
"Alain Quesnel" <alainsansspam (AT) logiquel (DOT) com> wrote

Quote:
You could do that within a stored proc, or create a UDF that returns a
table type.

What RDBMS are you using?

--

Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Matthew Pascoe" <mpascoe (AT) stocklogix (DOT) com.au> wrote in message
news:41723584$1 (AT) newsgroups (DOT) borland.com...
would like to write an SQL statement that will return a row for each day
that is in a date range that I provide.

For example if the date range is 01-10-04 to 04-10-04 I would like the
following result set:

Day(DateTime data type)
01-10-04
02-10-04
03-10-04
04-10-04


I need to use this as the base for creating a more complex query.

Thanks for any help.

Matthew







Back to top
Dennis Passmore
Guest





PostPosted: Mon Oct 18, 2004 12:47 pm    Post subject: Re: SQL to return a row for each day in a date range Reply with quote

Look up "between" in the Sql Books Online.


Dennis Passmore
Ultimate Software, Inc.
Back to top
Alain Quesnel
Guest





PostPosted: Mon Oct 18, 2004 4:23 pm    Post subject: Re: SQL to return a row for each day in a date range Reply with quote

Would something like this do the trick?

/*-------------*/
create function dbo.fnCreateDateList (@inStartDate datetime, @inEndDate
datetime)
returns @DateList table(ID int identity (1, 1), DateValue datetime)
as
begin
while @inEndDate >= @inStartDate
begin
insert into @DateList
values(@inStartDate)
set @inStartDate = @inStartDate + 1
end
RETURN
end
/*-----------*/

And to test it:

select * from dbo.fnCreateDateList ('2004-12-23', '2005-01-03')

You could use some error processing to detect a start date greater than or
equal to the end date.


--

Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Matthew Pascoe" <mpascoe (AT) stocklogix (DOT) com.au> wrote

Quote:
SQL Server 2000
"Alain Quesnel" <alainsansspam (AT) logiquel (DOT) com> wrote in message
news:41726dc5$1 (AT) newsgroups (DOT) borland.com...
You could do that within a stored proc, or create a UDF that returns a
table type.

What RDBMS are you using?

--

Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Matthew Pascoe" <mpascoe (AT) stocklogix (DOT) com.au> wrote in message
news:41723584$1 (AT) newsgroups (DOT) borland.com...
would like to write an SQL statement that will return a row for each day
that is in a date range that I provide.

For example if the date range is 01-10-04 to 04-10-04 I would like the
following result set:

Day(DateTime data type)
01-10-04
02-10-04
03-10-04
04-10-04


I need to use this as the base for creating a more complex query.

Thanks for any help.

Matthew









Back to top
Matthew Pascoe
Guest





PostPosted: Thu Oct 21, 2004 4:05 am    Post subject: Re: SQL to return a row for each day in a date range Reply with quote

Hi Alain,

Thats a great solution, I'll have a play around with it.

Thanks for taking the time to help me out.

Kind Regards,
Matthew


"Alain Quesnel" <alainsansspam (AT) logiquel (DOT) com> wrote

Quote:
Would something like this do the trick?

/*-------------*/
create function dbo.fnCreateDateList (@inStartDate datetime, @inEndDate
datetime)
returns @DateList table(ID int identity (1, 1), DateValue datetime)
as
begin
while @inEndDate >= @inStartDate
begin
insert into @DateList
values(@inStartDate)
set @inStartDate = @inStartDate + 1
end
RETURN
end
/*-----------*/

And to test it:

select * from dbo.fnCreateDateList ('2004-12-23', '2005-01-03')

You could use some error processing to detect a start date greater than or
equal to the end date.


--

Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Matthew Pascoe" <mpascoe (AT) stocklogix (DOT) com.au> wrote in message
news:4172ff1e$1 (AT) newsgroups (DOT) borland.com...
SQL Server 2000
"Alain Quesnel" <alainsansspam (AT) logiquel (DOT) com> wrote in message
news:41726dc5$1 (AT) newsgroups (DOT) borland.com...
You could do that within a stored proc, or create a UDF that returns a
table type.

What RDBMS are you using?

--

Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Matthew Pascoe" <mpascoe (AT) stocklogix (DOT) com.au> wrote in message
news:41723584$1 (AT) newsgroups (DOT) borland.com...
would like to write an SQL statement that will return a row for each
day
that is in a date range that I provide.

For example if the date range is 01-10-04 to 04-10-04 I would like the
following result set:

Day(DateTime data type)
01-10-04
02-10-04
03-10-04
04-10-04


I need to use this as the base for creating a more complex query.

Thanks for any help.

Matthew











Back to top
Chuck Hall
Guest





PostPosted: Wed Oct 27, 2004 6:18 pm    Post subject: Re: SQL to return a row for each day in a date range Reply with quote


I created a table with such dates by creating and joining three small tables, called TRACKER.MONTHS (MONTH field contains 1 to 12), TRACKER.DAYS (DAY field contains 1 to 31) and TRACKER.YEARS (field YEAR containing enough four digit years to meet my needs).

I create TRACKER.CALENDAR with the following query (Oracle syntax):

INSERT INTO TRACKER.CALENDAR SELECT TO_DATE(MONTH||'/'||DAY||'/'||YEAR,'MM/DD/YYYY') "FULLDATE"
FROM TRACKER.MONTHS,TRACKER.DAYS,TRACKER.YEARS
WHERE (MONTH IN (1,3,5,7,8,10,12))
OR ((MONTH IN (4,6,9,11)) AND (DAY<31))
OR ((MONTH=2) AND (DAY<29))
OR ((MONTH=2) AND (MOD(YEAR,4)=0) AND (DAY=29) AND ((MOD(YEAR,100)<>0) OR (MOD(YEAR,400)=0)))

This is called a "cartesian" join which joins all members of each table will all members of the other table(s). That is not often done intentionally.

Chuck Hall

"Wayne Niddery [TeamB]" <wniddery (AT) chaffaci (DOT) on.ca> wrote:
Quote:
Matthew Pascoe wrote:
would like to write an SQL statement that will return a row for each
day that is in a date range that I provide.

For example if the date range is 01-10-04 to 04-10-04 I would like the
following result set:

Day(DateTime data type)
01-10-04
02-10-04
03-10-04
04-10-04

Not possible to manufacture data that doesn't exist with SQL. You can do
this with a stored procedure, or if your database does not support stored
procedures, then you could also do it by creating a table that has all dates
for whatever periods you need to query, then SQL can select dates in a
specified range from that and join against other tables as necessary.

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"Nature abhors the vacuum tube." - J.R. Pierce, Bell Labs engineer who
coined the term 'transistor'




Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.