473,382 Members | 1,407 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,382 software developers and data experts.

SQL statement for generating rows of intermediate dates

Dear all,

Is there any DB2 SQL statement that could generate rows of intermediate
dates by providing the start and end dates??

If 2006-1-1 and 2006-1-31 are provided to the SQL, the output will look
like :

2006-1-1
2006-1-2
2006-1-3
...
...
...
2006-1-31

Thanks in advance!

Henry

Jan 25 '06 #1
5 1954
------------------------- Commands Entered -------------------------
SELECT start_date + (n2*10+n1) DAYS AS Inter_dates
FROM (VALUES (DATE('2006-1-1'), DATE('2006-1-31'))) Q (Start_date,
End_date)
, (VALUES 0,1,2,3,4,5,6,7,8,9) P1(n1)
, (VALUES 0,1,2,3,4,5,6,7,8,9) P2(n2)
WHERE (n2*10+n1) <= DAYS(End_date) - DAYS(Start_date)
ORDER BY
Inter_dates
;
--------------------------------------------------------------------

INTER_DATES
-----------
2006-01-01
2006-01-02
2006-01-03
2006-01-04
2006-01-05
2006-01-06
2006-01-07
2006-01-08
2006-01-09
2006-01-10
2006-01-11
2006-01-12
2006-01-13
2006-01-14
2006-01-15
2006-01-16
2006-01-17
2006-01-18
2006-01-19
2006-01-20
2006-01-21
2006-01-22
2006-01-23
2006-01-24
2006-01-25
2006-01-26
2006-01-27
2006-01-28
2006-01-29
2006-01-30
2006-01-31

31 record(s) selected.
Or, you can use recursive query.

Jan 25 '06 #2
Very intelligent thought

Jan 25 '06 #3
Henry wrote:
Dear all,

Is there any DB2 SQL statement that could generate rows of intermediate
dates by providing the start and end dates??

If 2006-1-1 and 2006-1-31 are provided to the SQL, the output will look
like :

2006-1-1
2006-1-2
2006-1-3
..
..
..
2006-1-31

CREATE FUNCTION dates(start DATE, end DATE)
RETURNS TABLE(dt DATE)
RETURN WITH rec(dt) AS (VALUES (start)
UNION ALL
SELECT dt + 1 DAY FROM rec
WHERE dt < end)
SELECT dt FROM rec;

SELECT dt, item, sales
FROM TABLE(dates(DATE('2003-01-01'),
DATE('2003-12-31'))) AS dates
LEFT OUTER JOIN sales ON dates.dt = sales.dt;

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Jan 25 '06 #4
Thanks you very much for yours' brilliant solutions!!

Henry

- 26 Jan 2006

Jan 26 '06 #5
A small change that eliminates the warning message...

But only good for a 10,000 years of dates.... Darn

CREATE FUNCTION DATES(START DATE, END DATE)
RETURNS TABLE(DT DATE)
RETURN
WITH REC(DT, LEVEL) AS
(
VALUES (START, 1)
UNION ALL
SELECT DT + 1 DAY, LEVEL + 1
FROM REC
WHERE DT + 1 DAY <= END
AND LEVEL < 3650000
)
SELECT DT FROM REC;

just my two cents... Enjoy

Jan 26 '06 #6

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

Similar topics

13
by: kieran | last post by:
Hi, I have the following SQL statement which is pulling a few details from a database. As you can see, there is only the one table from which i am creating a temporary copy. The reason I do...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
1
by: ajk | last post by:
Hi When doing a .NET compiler what are the benefits of generating ILAsm directly compared to generating another higher level language like C# first and then using the C# compiler? ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.