DB2 How to select dates for the previous 2 months - sql

I am using Oracle SQL developer on DB2 and have a date field stored as an integer e.g. 20210401
I want to bring back results for the last 2 months and have tried this:
select * from table where date > add_months(sysdate, -2)
This is producing error 206 saying it is not valid in the context used.
Does anyone know how to convert the data column or have an easier way to filter for the last 2 months

Use this:
select *
from table
where date > INT (TO_CHAR (CURRENT TIMESTAMP - 2 MONTH, 'YYYYMMDD'));

Related

Presto SQL get yyyymm minus 2 months

I am using Presto. I have an integer column (let's call the column 'mnth_nbr') showing year and month as: yyyymm. For instance, 201901. I want to have records showing all dates AFTER 201901 as well as 2 months before the given date. In this example, it would return 201811, 201812, 201901, 201902, 201903, etc. Keep in mind that my data type here is integer.
This is what I have so far (I do a self join):
select ...
from table 1 as first_table
left join table 1 as second_table
on first_table.mnth_nbr = second_table.mnth_nbr
where first_table.mnth_nbr <= second_table.mnth_nbr
I know this gives me all dates AFTER 201901, including 201901. But, I don't know how to add the 2 previous months (201811 and 201812)as explained above.
As far as the documentation, Presto DB date_parse function expects a MySQL-like date format specifier.
So the proper condition for your use case should be :
SELECT ...
FROM mytable t
WHERE
date_parse(cast(t.mnth_nbr as varchar), '%Y%m') >= date '2019-01-01' - interval '2' month
Edit
As commented by Piotr, a more optimized expression (index-friendly) would be :
WHERE
mnth_nbr >= date_format(date '2019-01-01' - interval '2', '%Y%m')
Something like this would help. first parse your int to date
date_parse(cast(first_table.mnth_nbr as varchar), 'yyyymm') > date '2019-01-01' - interval '2' month
please keep in mind that you may encounter with indexing issues with this approach.

current month record should be display from table

please assist me to correct it.i want to show current month data i tried this but its showing error the literal is too long :
number date
10 20-Jan-2018
20 30-Oct-2018
30 24-Sep-2018
24 01-Oct-2018
select number
from table
where date <= to_char(sysdate,'mm');
It looks like your date column contains text. You should ideally always store date information in proper date columns. As a workaround, we can go in the other direction and use TO_DATE on your date column, to compare it to the first of the current month:
SELECT number
FROM yourTable
WHERE TO_DATE(date, 'dd-mon-yyyy') < TRUNC(sysdate, 'mm');

Get month,days difference between two date columns

I'm trying to fix some problems in my database and i want to re-calculate column in my db based on other 2 date columns. This col is float and i want to get the difference between 2 dates in months with decimal point for days.
For example if i have 2 dates '2016-01-15', '2015-02-01' the difference should be 12.5 best of 12 months differences and 0.5 for the remaining 15 days
Here is what i tried so far based on my searches but i think there is something i'm missing as it tells me there is an error with my date col as it doesn't exist
Select EXTRACT(year FROM vehicle_delivery(date, vehicle_received_date))*12 + EXTRACT(month FROM vehicle_delivery(date, vehicle_received_date));
Where vehicle_delivery is my table name & date is my end date and vehicle_received_date is my start date
same thing happes with this sql :
select extract('years' from vehicle_delivery) * 12 + extract('months' from vehicle_delivery) + extract('days' from vehicle_delivery) / 30
from (select age(date::timestamp, vehicle_received_date::timestamp)) a;
The SQL should look like this:
select extract(year from diff) * 12 + extract(month from diff) + extract(day from diff) / 30
from (select age(date::timestamp, vehicle_received_date::timestamp) as diff
from vehicle_delivery
) vd;
I don't know what the purpose of the / 30 is, but you appear to want it.
Notes:
The FROM clause references the table.
The first argument in extract() is a keyword, not a string.
You want to reference the age() value in the extract().
extract() returns an interval, so it is rather redundant to take out the parts (only needed if you want them in separate columns).

date comparison oracle sql

how to compare joining date with current date and if it is less than or equal to 45 days then select those details and display in oracle sql, what to do if joining date and current date in different format like (joining date 12/03/2015 and current date 01-MAR-17)?
That would be something like this (if you need rows for those who joined 45 days or more ago):
select *
from your_table
where joining_date <= trunc(sysdate) - 45;
I luckily got output, query is
select COL_NAME from TABLE_NAME where (to_date(SYSDATE)- to_date(COL_NAME,'dd/mm/yyyy'))<=60

PL/SQL to subtract two dates

I use SQL Server 2012, and in my table I have columns id, date, days.
Column date is formatted dd-mm-yyyy.
I need PL/SQL code to fetch today date and find difference between date column and store it in days column
Example:
fetch today date and find difference of stored date from the table
id date days
1 01-12-2015 1
2 30-11-2015 2
I need PL/SQL code.
You may use DATEDIFF:
SELECT DATEDIFF(day,'2015-06-05','2015-08-05') AS DiffDate
For your case it should be:
INSERT INTO table_name (days)
SELECT DATEDIFF(day,table_name.date,GETDATE());
SQL Server Date Functions
Use DATEDIFF and the LEAD function to compare against the next row.
SELECT DATEDIFF(d,date,LEAD(date) OVER (ORDER BY id))
FROM yourtable
For comparison against today's date
SELECT DATEDIFF(d,date,GETDATE())
FROM yourtable