Netezza - find the first date of the prior quarter - date

I'm trying to get the first day of the previous quarter from today's date however I can't find logic for Netezza SQL.
For SQL Server I could use the following:
select dateadd(quarter, datediff(quarter, 0, getdate()) - 1, 0)
There doesn't appear to be an equivalent of datediff in Netezza, any suggestions would be greatly appreciated

=> select now(), (date_trunc('quarter', now()) - interval ('3 months'))::date as result;
NOW | RESULT
---------------------+------------
2022-09-02 13:09:05 | 2022-04-01
(1 row)

Based on a similar answer here I was able to adapt my code to the following:
WHERE
TABLE_A.DATE_FIELD BETWEEN (SELECT TO_DATE(TO_CHAR(TO_DATE(TO_CHAR(CURRENT_DATE, 'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ')) AND (SELECT TO_DATE(TO_CHAR(CURRENT_DATE,'YYYYQ'),'YYYYQ'))

Related

How to parameterize a query using INTERVAL

The below query is syntactically correct, but it's not returning anything. I'm trying to find all data older than ? months. I've been testing with 1 month, or even 1 day, but I don't get any data. Is there anything wrong with this query?
SELECT * FROM detail t
WHERE t.job_start_time < now() - (? * INTERVAL '1 MONTHS')
I prefer using make_interval():
SELECT *
FROM detail t
WHERE t.job_start_time < now() - make_interval(months => ?)

How to select data in 2 month(from now) use postgresql?

I'm new to sql and database work . Now I want select data in 2 month from now . the key is xxdate lookslike 2019-4-11
like:
select * from table where date > now() - 2 month
but I don't know the correct way to express it. can someone help?
You can use this query:
SELECT * FROM table WHERE date > (current_date - interval '2 month')::date;

PostgreSQL Query: Need to add 7 hours to NOW() to adjust the time to GMT to match the other side of my calcuation

Hello Overflow Community!
Q: Need to add 7 hours to NOW() to adjust the time to GMT. This calculation is being used in the WHERE statement of my query. I am using PostgreSQL in SAP HANA Studio Version: 2.3.28.
I have researched similar questions in Stackoverflow and used variations of syntax found in my research, but I cannot get past SQL syntax errors.
I cannot identify what is incorrect about my my approach. Can someone help me fix this query?
`SELECT
a.ACCOUNTID,
a.CASENUMBER,
a.ID,
a.STATUS,
a.SUBJECT,
a.CREATEDDATE_GMT,
b.NAME,
b.OWNERID`
`FROM "SFDC"."CASE" AS a
LEFT JOIN "SFDC"."GROUP" AS b ON a.OWNERID = b.ID`
`WHERE CAST(a.CREATEDDATE_GMT AS DATE) = CAST(DATEADD('hour', 7, NOW()) AS DATE);
`
I also tried these other 4 variation in my WHERE statement, but received syntax errors.
CAST(a.CREATEDDATE_GMT AS DATE) = ADD_DAYS(hour, 7, CAST(NOW()AS DATE))
CAST(a.CREATEDDATE_GMT AS DATE) = CAST(NOW()AS DATE) + CAST((7 || ' hours') AS INTERVAL)
CAST(a.CREATEDDATE_GMT AS DATE) = CAST(NOW()AS DATE) + INTERVAL '7 hours'

How to calculate end of the month in Postgres?

How to calculate end of the month in Postgres? I have table with column date datatype. I want to calculate end of the month of every date. For Eg. In the table there values like "2015-07-10 17:52:51","2015-05-30 11:30:19" then end of the month should be like 31 July 2015,31 May 2015.
Please guide me in this.
How about truncating to the beginning of this month, jumping forward one month, then back one day?
=# select (date_trunc('month', now()) + interval '1 month - 1 day')::date;
date
------------
2015-07-31
(1 row)
Change now() to your date variable, which must be a timestamp, per the docs. You can then manipulate this output (with strftime, etc.) to any format you need.
Source
SELECT TO_CHAR(
DATE_TRUNC('month', CURRENT_DATE)
+ INTERVAL '1 month'
- INTERVAL '1 day',
'YYYY-MM-DD HH-MM-SS'
) endOfTheMonth
Hi I tried like this and it worked
Date(to_char(date_trunc('month'::text, msm013.msa011) + '1 mon - 1 day '::interval , 'DD-MON-YYYY') )
Thanks a lot!!

Create date efficiently

On Pavel's page is the following function:
CREATE OR REPLACE FUNCTION makedate(year int, dayofyear int)
RETURNS date AS $$
SELECT (date '0001-01-01' + ($1 - 1) * interval '1 year' + ($2 - 1) * interval '1 day'):: date
$$ LANGUAGE sql;
I have the following code:
makedate(y.year,1)
What is the fastest way in PostgreSQL to create a date for January 1st of a given year?
Pavel's function would lead me to believe it is:
date '0001-01-01' + y.year * interval '1 year' + interval '1 day';
My thought would be more like:
to_date( y.year||'-1-1', 'YYYY-MM-DD');
Am looking for the fastest way using PostgreSQL 8.4. (The query that uses the date function can select between 100,000 and 1 million records, so it needs speed.)
Thank you!
I would just use the following, given that year is a variable holding the year, instead of using a function:
(year || '-01-01')::date
Btw. I can't believe that this conversion is your bottleneck. But maybe you should have a look at generate_series here (I don't know your usecase).
select current_date + s.a as dates from generate_series(0,14,7) as s(a);
dates
------------
2004-02-05
2004-02-12
2004-02-19
(3 rows)
Using to_date() is even simpler than you expect:
> select to_date('2008','YYYY');
to_date
------------
2008-01-01
(1 row)
> select to_date(2008::text,'YYYY');
to_date
------------
2008-01-01
(1 row)
Note that you still have to pass the year as a string, but no concatenation is needed.
As suggested by Daniel, in the unlikely case that this conversion is a bottleneck, you might prefer to precompute the function and store in a table. Eg:
select ynum, to_date( ynum ||'-01-01', 'YYYY-MM-DD') ydate
from generate_series(2000,2009) as ynum;
If there are a few years (and hence no need of indexes), you might even create the table dinamically for the scope of each query, with the new WITH.