how to get each and every Date in between selected date (in DB2) - date

Dear StackOverflow Community,
as a New to DB2 ,i have a a query
may be its a very basic question for you, please share your knowledge.
i have a start date and End Date.
I need a list of each and every date in between.
Its ok with me ,if it creates a temp table no issue.
Thanks in Advance

You can generate the dates between start and end dates by using Recursive CTE expression. Try below code
with cte(your_columns,startdate,enddate)
as (select your_columns,startdate,enddate,startdate
as derDate
from yourTable
union all
select your_columns,startdate,enddate,derDate+1
from cte where
derDate<=endDate)
select * from cte

Related

Add dates ranges to a table for individual values using a cursor

I have a calendar table called CalendarInformation that gives me a list of dates from 2015 to 2025. This table has a column called BusinessDay that shows what dates are weekends or holidays. I have another table called OpenProblemtimeDiffTable with a column called number for my problem number and a date for when the problem was opened called ProblemNew and another date for the current column called Now. What I want to do is for each problem number grab its date ranges and find the dates between and then sum them up to give me the number of business days. Then I want to insert these values in another table with the problem number associated with the business day.
Thanks in advance and I hope I was clear.
TRUNCATE TABLE ProblemsMoreThan7BusinessDays
DECLARE #date AS date
DECLARE #businessday AS INT
DECLARE #Startdate as DATE, #EndDate as DATE
DECLARE CONTACT_CURSOR CURSOR FOR
SELECT date, businessday
FROM CalendarInformation
OPEN contact_cursor
FETCH NEXT FROM Contact_cursor INTO #date, #businessday
WHILE (##FETCH_STATUS=0)
BEGIN
SELECT #enddate= now FROM OpenProblemtimeDiffTable
SELECT #Startdate= problemnew FROM OpenProblemtimeDiffTable
SET #Date=#Startdate
PRINT #enddate
PRINT #startdate
SELECT #businessday= SUM (businessday) FROM CalendarInformation WHERE date > #startdate AND date <= #Enddate
INSERT INTO ProblemsMoreThan7BusinessDays (businessdays, number)
SELECT #businessday, number
FROM OpenProblemtimeDiffTable
FETCH NEXT FROM CONTACT_CURSOR INTO #date, #businessday
END
CLOSE CONTACT_CURSOR
DEALLOCATE CONTACT_CURSOR
I tried this code using a cursor and I'm close, but I cannot get the date ranges to change for each row.
So if I have a problemnumber with date ranges between 02-07-2018 and 05-20-2019, I would want in my new table the sum of business days from the calendar along with the problem number. So my output would be column number PROB0421 businessdays (with the correct sum). Then the next problem PRB0422 with date ranges of 11-6-18 to 5-20-19. So my output would be PROB0422 with the correct sum of business days.
Rather than doing this in with a cursor, you should approach this in a set based manner. That you already have a calendar table makes this a lot easier. The basic approach is to select from your data table and join into your calendar table to return all the rows in the calendar table that sit within your date range. From here you can then aggregate as you require.
This would look something like the below, though apply it to your situation and adjust as required:
select p.ProblemNow
,p.Now
,sum(c.BusinessDay) as BusinessDays
from dbo.Problems as p
join dbo.calendar as c
on c.CalendarDate between p.ProblemNow and p.Now
and c.BusinessDay = 1
group by p.ProblemNow
,p.Now
I think you can do this without a cursor. Should only require a single insert..select statement.
I assume your "businessday" column is just a bit or flag-type field that is 1 if the date is a business day and 0 if not? If so, this should work (or something close to it if I'm not understanding your environment properly).:
insert ProblemsMoreThan7BusinessDays
(
businessdays
, number
)
select
number
, sum( businessday ) -- or count(*)
from OpenProblemtimeDiffTable op
inner join CalendarInformation ci on op.problem_new >= ci.[date]
and op.[now] <= ci.[date]
and ci.businessday = 1
group by
problem_number
I usually try to avoid the use of cursors and working with data in a procedural manner, especially if I can handle the task as above. Dont think of the data as 1000's of individual rows, but think of the data as only two sets of data. How do they relate?

GROUP BY in a JOIN?

I have a data table and a calendar table. The date table only contains the fields MonthName and FiscalYearName. My calendar table has each day for each year. How can I write the JOIN to make sure I am not getting duplicated data due to the inability to JOIN on a date? Right now am I just linking on those two fields and I am getting duplicate data.
Could you elaborate on the exact issue?
If month and year are the only fields, why exactly do you need to join on Calendar? To get rid of duplicates, the easiest solution might be to use a 'DISTINCT'.
SELECT DISTINCT dat.year, dat.month, *
FROM date dat
JOIN calendar cal
ON dat.year = cal.year
AND dat.month = cal.month
Edit;
Extra query to just show the MonthNum;
This query uses a common table expression which gets the distinct month names and their number only once, then joins the date table on this cte.
WITH cteCalendar AS
(
SELECT DISTINCT MonthName, MonthNum FROM calendar
)
SELECT cte.MonthNum, dat.*
FROM date dat
JOIN cteCalendar cte
AS dat.MonthName = cte.MonthName

Manipulations with dates in Sqlite3

We are working with sqlite3 and have encountered one problem.
We have a database for library with several tables, in particular - items table and loans table. When we insert a new item into the loans table, we have to save the Start date and Due date of the loan. The starting date is select Date('now') and the Due date should be calculated as the Start date + exact number of days taken from the items table (= loanduration).
We try to do it with following query:
update loans set Sdate=(SELECT date('now')) where CID=NEW.CID and IID=NEW.IID;
update loans set Ddate= select date(NEW.Sdate, '+' || (select loanduration from items where items.IID=NEW.IID) where CID=NEW.CID and IID=NEW.IID day|| 'day');
However, it doesnt work and we cant find the proper solution.
Can anybody help, please?
According to the documentation, the modifier must be +NNN days:
> SELECT date('now', '+3day');
> SELECT date('now', '+3 day');
2014-03-12

To Pull records between two dates in db2

I tried below two ways they not working
Select * from Table
where SERV_DATE BETWEEN '03/01/2013'AND
'03/31/2013'
ALSO This is not working
Select * from Table
where SERV_DATE BETWEEN DATE('03/01/2013') AND
DATE('03/31/2013')
What should be the correct format ?
Did you tried what NealB suggested? The reason for not accepting 03/01/2013 as an entry date format is, that it is region dependent in the US it is March 1, 2013 an in the UK it is January 3, 2013. So without considering the local, it is not certain, what the actual date is.
"why would db2 give error on the same format and will go well when given different format" - Don't forget, that db2 is an old lady and as all old ladies she has peculiarities. You just get used to it and there will be an happy ending.
SELECT * FROM tableName WHERE date(modifiedBy_date) between '2017-07-28' AND '2017-08-01';
Works cool for DB2.
Select * from Table
where SERV_DATE BETWEEN DATE('2013-03-01') AND DATE('2013-03-31');
Worked for me.
Select * from Table
where (SERV_DATE BETWEEN '03/01/2013'AND
'03/31/2013')
Select * from Table
where (SERV_DATE BETWEEN '2013-03-01'AND
'2013-03-31')
select count(*) from TABLE where time_stamp BETWEEN DATE('2018-01-01') AND DATE('2018-01-31');
Here time_stamp is field name and copy your timestamp filed name instead of time_stamp.

Oracle to_char conversion causing hour offset?

I want to run this issue by those who know more about PL/SQL and T-SQL than I do. Below is some code that I inherited and it's returning dates that are 1 hour off. When I remove the "to_char(i.UPDATE_DT, ''YYYY-MM-DD HH24:MI:SS.FF3'')" in the select statement of the remote query the hour offset goes away. I'm worried about other impacts this change could have, so my question is why would someone convert an Oracle time to a string via an OpenQuery and then use T-SQL convert in the select statement? What am I missing here and why would the to_char cause an offset (if it is the reason)?
SELECT
CSE_ID AS ID,
OTHR_CSE_ID AS Case_Num,
convert(datetime,UPDATE_DT,121) AS Export_Time
FROM
OpenQuery( OracleTbl, '
SELECT c.CSE_ID, r.OTHR_CSE_ID, to_char(i.UPDATE_DT,''YYYY-MM-DD HH24:MI:SS.FF3'') UPDATE_DT
FROM AE_CSES c
INNER JOIN CSES i ON i.CASE_ID = c.CSE_ID AND i.ACTION_CD = ''INS''
INNER JOIN OTH_CSE_REFS r ON r.CSE_ID = c.CSE_ID AND r.OTH_CS_REF_SEQ_NBR = 1 AND r.OTHER_SOURCE_TYPE = ''SIE''
') AS i
Simplistic answer but; Daylight savings issue between one server and the other?