SQL to find date larger than another - sql

I am trying to run a query on a DB2 database that is included in an INNER JOIN that will show everything that is older than 1 year.
Here is the code I am running:
month(T4.BIRTH_DT) > (SELECT CURRENT DATE - (DAY(CURRENT DATE) DAYS) - 11 MONTH + 1 DAY from SYSIBM.SYSDUMMY1 )
But I get the following error:
SQL0401N The data types of the operands for the operation ">" are not compatible. SQLSTATE=42818
To me it looks like both should be dates but I cannot for the life of me figure out what the format should be.
Thanks for any help.

I think you can use a simpler version:
T4.BIRTH_DT < CURRENT DATE - 1 YEARS

Simplified answer
month(T4.Birth_DT) = (MONTH(CURRENT DATE - 11 MONTHS))

Related

Trying to accommodate relative defined date, such as 5 days ago, into my fixed date condition in PostgreSQL

I'm trying to condition my WHERE clause to accommodate relatively defined dates into my date filter. I'm pretty confused what type I need to use, if it's CONVERT or TO_DATE function, or if I need to put a CASE WHEN statement into my code.
This is the code that I have written so far:
WHERE event_create_verified_user_success.created_at_utc_date
BETWEEN DATE '2021-11-29' AND DATE '2021-12-05'
And this is the condition of the activity I need to finish:
If the desired date-period is not set manually using fixed dates like from “2021-11-29”
to “2021-12-05”, how would you change the where-clause to consider all data from relative
defined dates: “consider messages created between 10 days and 5 days ago (inclusive)”
I've only started PostgreSQL yesterday and the last time I've handle SQL was probably 4 years ago so I'm pretty confused at how much SQL has changed since then.
Thank you so much for helping!
The basic syntax hasn't really changed in the last 4 years (or even 15 years).
You can use current_date to obtain "today's date". You can subtract days from that
where ... between current_date - 10 and current_date - 5
If created_at_utc_date is a timestamp (= date and time) rather than a date (=date without time) it's better to use a range query though:
where created_at_utc_date >= current_date - 10
and created_at_utc_date < current_date - 4
Note the < combined with the next day you want to compare with.

DB2 How to select dates for the previous 2 months

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'));

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.

Fetch records with a where condition that specifies fetch records for this week in DB2

I am using DB2. I want to know the number of applications (Records/row) since the last seven days including today and my date column is ReceivedDate.
I basically want to write a query that intents to do the following: (DB2), please help me.
Select count(*) from Applications WHERE ReceivedDate is within 7 days
Try this query, which uses CURRENT DATE:
SELECT COUNT(*)
FROM Applications
WHERE ReceivedDate > CURRENT DATE - 7 DAY;
This answer assumes that a received date would never be in the future (since it can't have yet happened).
You can try below -
Select count(*) from Applications
WHERE ReceivedDate>CURRENT DATE - 7 Days and ReceivedDate<=CURRENT DATE

Make calc on dates using informix db

I want to put the following condition in my query ,but i don't know the correct syntax in informix to do that .
At least one year passed on his work date ..
So
I try some thing like that
b.work_date - CURRENT >= 12 -- 12 month
How to do that ?
You can do:
b.work_date <= CURRENT - 12 UNITS MONTH
You need to be careful with the CURRENT - 12 UNITS MONTH approach. It does not take the leap-day in February into account, and would explode with an Invalid day in date error if you ran it on 2012-04-29.
It is safer to write
b.work_date < TODAY - 365