473,725 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1972
------------------------- 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(DAT E('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
1909
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 this is because in the table i only have the 'standIn' listed by integer and i want to return the 'standIn' by name. I hope this is clear enough.
4
28823
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 select statement). I'm mostly a SQL hobbiest, so it's possible that I am not doing this in the most efficient manner. Anyone care to comment on this with relation to the following example (is there a way to acheive this without re-stating the...
2
3208
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 : INSERT INTO temp_tab VALUES (1,2,3)
1
1565
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? Round-tripping will not work and debugging may be easier, any other reasons why it would be prefered to generate ILAsm? tia/ajk
0
8752
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
9401
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...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.