Make calc on dates using informix db - sql

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

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.

How do I find the first and last day of the previous month using SQL?

I am using IBM Data Studio to access data on an AS400, I cannot figure out how to programmatically find the first and last date of the previous month to use in an BETWEEN clause. For example, if I ran it today it would return 20211201 and 20211231. I have done some other date math in the past but this one is beyond me. Below is an example of a date function I have used successfully in this version of SQL.
{DEC(DATE(DAYS((CURRENT_DATE) - 15 DAY)))}
Try
DATE(YEAR(CURRENT_DATE)||'-'||MONTH(CURRENT_DATE)||'-1') - 1 MONTH
and
DATE(YEAR(CURRENT_DATE)||'-'||MONTH(CURRENT_DATE)||'-1') - 1 DAY

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

SQL to find date larger than another

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

Teradata Change format of Week Number

I'm pretty new to SQL so I hope this isn't a dumb question, tried to google but couldn't find anything.
I'm summing sales of departments per week in SQL and am using TD_SYSFNLIB.WEEKNUMBER_OF_YEAR (trans_dt) to get the week number.
I think everything is working except I'd like to change the format of the weeks to the start date of the week, e.g. week 1 = 1/4/15
Also, i'm not sure how to handle the very first of the year week 0 since I think that should be grouped up with week 52 of last year.
The following date math trick should get you Beginning of Week as an actual date without having to join to the SYS_CALENDAR view or using a function:
SELECT CURRENT_DATE - ((CURRENT_DATE - DATE '0001-01-07) MOD 7) AS BOW;
Starting with TD14 there's NEXT_DAY which returns the following weekday, if you subtract 7 days you get the previous day:
next_day(trans_dt - 7, 'sunday')