How to multiply months to current date upto 1 year - db2

I wanted to show the forecast dates with the current date plus frequency up to one year in DB2.
date :Current date
if frequency is :2
upto : 2020-01-01
output be like :
2019-05-22,
2019-07-22,
2019-09-22,
2019-11-22

Try the following RCTE:
with t(dt) as (
values current date
union all
select dt + 2 month
from t
where year(dt + 2 month) = year(current date)
)
select dt
from t;

Related

Only get rows based on flag for weekend / weekday / both

I have a whole bunch of tariffs, some work on weekends, some work on weekdays some on both. Sometimes I'll be querying on NOW() but sometimes I'll be querying on datetime column.
id | Weekday | Weekend | Price
1 | 1 | 0 | 0.04
2 | 0 | 1 | 0.02
date
2020-04-15 00:00:00
2012-04-16 00:00:00
The date is from another table and is not related to the Price / days of week.
I know I can get the weekend dates by
SELECT * FROM tariff where EXTRACT(ISODOW FROM date) IN (6,7)
however I can't think of how I'd get rows that are either weekend / weekdays or both given a date.
** edit **
Updated the tables to show the dates are seperate. What I'm trying to get is the tariff that corresponds to the date in that table, whether it's on a week day or a weekend (or both but I can extrapolate that).
The weekend 1 is the tariff that is used for weekends, weekdays 1, all days is both.
Cannot give you a query, supply anything to query. Nor can we be sure that the columns Weekday and Weekend mean as you didn't tell us. But if we take them as boolean indicator where 1 means desired may some thing like will work for you.
select ...
from ...
where ...
and ( (weekday = 1 and weekend =1)
or (weekday = 1 and extract(isodow from date) not in (6,7))
or (weekend = 1 and extract(isodow from date) in (6,7))
) ;

DB2: Bi-monthly query for a DB2 report

I am currently writing a Crystal Report that has a DB2 query as its backend. I have finished the query but am stuck on the date portion of it. I am going to be running it twice a month - once on the 16th, and once on the 1st of the next month. Here's how it should work:
If I run it on the 16th of the month, it will give me results from the 1st of that same month to the 15th of that month.
If I run it on the 1st of the next month, it will give me results from the 16th of the previous month to the last day of the previous month.
This comes down a basic bi-monthly report. I've found plenty of hints to do this in T-SQL, but no efficient ways on how to accomplish this in DB2. I'm having a hard time wrapping my head around the logic to get this to consistently work, taking into account differences in month lengths and such.
There are 2 expressions for start and end date of an interval depending on the report date passed, which you may use in your where clause.
The logic is as follows:
1) If the report date is the 1-st day of a month, then:
DATE_START is 16-th of the previous month
DATE_END is the last day of the previous month
2) Otherwise:
DATE_START is 1-st of the current month
DATE_END is 15-th of the current month
SELECT
REPORT_DATE
, CASE DAY(REPORT_DATE) WHEN 1 THEN REPORT_DATE - 1 MONTH + 15 ELSE REPORT_DATE - DAY(REPORT_DATE) + 1 END AS DATE_START
, CASE DAY(REPORT_DATE) WHEN 1 THEN REPORT_DATE - 1 ELSE REPORT_DATE - DAY(REPORT_DATE) + 15 END AS DATE_END
FROM
(
VALUES
DATE('2020-02-01')
, DATE('2020-02-05')
, DATE('2020-02-16')
) T (REPORT_DATE);
The result is:
|REPORT_DATE|DATE_START|DATE_END |
|-----------|----------|----------|
|2020-02-01 |2020-01-16|2020-01-31|
|2020-02-05 |2020-02-01|2020-02-15|
|2020-02-16 |2020-02-01|2020-02-15|
In Db2 (for Unix, Linux and Windows) it could be a WHERE Condition like
WHERE
(CASE WHEN date_part('days', CURRENT date) > 15 THEN yourdatecolum >= this_month(CURRENT date) AND yourdatecolum < this_month(CURRENT date) + 15 days
ELSE yourdatecolum > this_month(CURRENT date) - 1 month + 15 DAYS AND yourdatecolum < this_month(CURRENT date)
END)
Check out the THIS_MONTH function - there are multiple ways to do it. Also DAYS_TO_END_OF_MONTH might be helpful

D+14 days or D-14 days based on value in colum

I want to be able to return cars who have a registration date + 14 days and do this every year.
We tried with this:
select *
from cars
where date(date_part('year', current_date)||'-'||date_part('month',
registrationdate)||'-'||date_part('day', registrationdate)) =
current_date + interval '14 days';
But we got problem with year crossing, for example if I have a registration date at 2018-12-20 it won't work.
There is a better solution?
For example, I got this in data :
id | registrationdate
1 | 2017-12-20
2 | 2018-01-15
and I want to be able with a sql request to return registration date + 14 days, every year.
For our example, it needs to return first row when I launch a request on 2018-01-03, 2019-01-03, 2020-01-03, ... and return second row when I launch request 2018-01-29, 2019-01-29, 2020-01-29, ...
I give you the solution, if it can help someone :
select *
from (
select *,
(extract(year from age(registrationdate)) + 1) * interval '1 year' + interval '14 days' + registrationdate "next_rd_day"
from cars
) as cars_with_upcoming_registration_date
where
extract(month from age(current_date, next_rd_day))=0 and extract(day from
age(current_date, next_rd_day))=0
It creates a new column with next occurence based on desired interval and in where condition, you just have to test if month and day are same and it's ok.
Too, it's working great with leap year and date near end of the year.

To get the month based on date

If the days are less than 15 th of current month then I should get Current month , and after 15 th the. On the should roll to next month.
I m using db2 db
select case when day(current date)<15 then month(current date) else month(current date + 1 month) end from your table

db2 - Get Week of the Month

Hello guys I need know the number of the week of a month
For example:
Date | WeekOfTheMonth
2015-04-15 | 3
2015-03-01 | 1
2015-01-08 | 2
Beacuse in docs only see Week of the year
Thanks
Test this SQL query
SELECT KURRENT - FIRSTY + 1 FROM ( -- AVOID CURRENT AND FIRST KEYWORD
SELECT
WEEK_ISO(DATE(1) + (YEAR(date_colum)-1) YEARS + (MONTH(date_colum)-1) MONTHS )AS FIRSTY ,
WEEK_ISO(date(date_column)) AS KURRENT
FROM TEST_DATE_TABLE
) AS T
Try this:
SELECT week_iso(current date) -
week_iso(last_day(current date) - 1 month + 1 day)
FROM sysibm.sysdummy1
You don't mention your platform, but VARCHAR_FORMAT has a W format string, which gives the week of the month:
Week of the month (1-5), where week 1 starts on the first day of the
month and ends on the seventh day.
SELECT VARCHAR_FORMAT(CURRENT TIMESTAMP, 'W') FROM SYSIBM.SYSDUMMY1