Postgresql query by year - postgresql

I am trying to query the Postgres table by only Year
SELECT * FROM events WHERE date_part('year', date) = date_part('year', CURRENT_DATE);
when I pass the CURRENT_DATE it is working, but when I pass the '2019', It is not working.
how to do the same using flask-sqlalchemy.

The date_part function returns integer - not string.
This should work:
SELECT * FROM events WHERE date_part('year', date) = 2019;
Best regards,
Bjarni

Related

Postgres - Pass dynamically generated date to where clause

I need to generate series of date till current_date based on job's last run date
last run date ='2022-10-01'
current date = '2022-10-05'
generate date like
varchar dynamic_date = '2022-10-01','2022-10-02','2022-10-03','2022-10-04','2022-10-05'
and pass to where to clause
select *
from t1
where created_date in (dynamic_date)
this is not allowed as dynamic_date is varchar and created_date is date column
trying to find efficient way to do this
You can use generate_series()
select *
from t1
where created_date in (select g.dt::date
from generate_series(date '2022-10-01',
current_date,
interval '1 day') as g(dt)
)
Or even simpler:
select *
from t1
where created_date >= date '2022-10-01'
and created_date <= current_date

PostgreSQL Time Dimension (By Hours and Days) Error

I am am building a Time Dimension table in PostgreSQL with DATE_ID and DATE_DESC.
My T-SQL (works perfectly) script is:
set DATEFIRST 1
;WITH DATES AS (
SELECT CAST('2019-01-01 00:00:00.000' AS datetime) AS [DATE]
UNION ALL
SELECT DATEADD(HH,1,[DATE])
FROM DATES
WHERE DATEADD(HH,1,[DATE]) <= CAST('2019-12-31' AS datetime)
)
SELECT
DATE_ID, DATE_DESC
from
(
SELECT
CONVERT(int, CONVERT(char(8), DATE, 112)) AS DATE_ID,
DATE AS DATE_DESC
FROM
DATES)a
order by 1
OPTION (MAXRECURSION 0)
At the moment Im trying to convert this code to PostgreSQL readable one and it does not work..
Here is mine at the moment:
set EXTRACT(DOW FROM TIMESTAMP '2019-01-01 00:00:00.000')+1
;WITH DATES AS (
SELECT CAST('2019-01-01 00:00:00.000' AS timestamp) AS DATE
UNION ALL
SELECT CURRENT_DATE + INTERVAL '1 hour'
FROM DATES
WHERE CURRENT_DATE + INTERVAL '1 hour' <= CAST('2019-12-31' AS timestamp)
)
SELECT DATE_ID, DATE_DESC from
(SELECT cast(to_char((DATE)::TIMESTAMP,'yyyymmddhhmiss') as BIGInt) AS DATE_ID,
DATE AS DATE_DESC
FROM
DATES)a
order by 1
OPTION (MAXRECURSION 0)
I need all the hours (24h) between 2019-01-01 and 2019-12-31 . At the moment I think OPTION (MAXRECURSION 0) and set EXTRACT(DOW FROM TIMESTAMP '2019-01-01 00:00:00.000')+1 is not working properly.
Its a problem of Recursive CTE, In Postgresql, your desired query will be like below
WITH recursive DATES AS (
SELECT CAST('2019-01-01 00:00:00.000' AS timestamp) AS date_
UNION ALL
SELECT date_ + INTERVAL '1 hour'
FROM DATES
WHERE date_ + INTERVAL '1 hour' <= CAST('2019-12-31' AS timestamp)
)
SELECT DATE_ID, DATE_DESC from
(SELECT cast(to_char((date_)::TIMESTAMP,'yyyymmddhhmiss') as BIGInt) AS DATE_ID,
date_ AS DATE_DESC
FROM
DATES)a
order by 1
DEMO

TO_DATE ('01/01/1980','dd/mm/yyyy') is not working when calling oracle database from Java

In SQL Navigator(Oracle) the following three queries are working
SELECT * FROM some_table a where TO_DATE (A.CREATE_DATE, 'dd/mm/yyyy') BETWEEN TO_DATE ('01/01/1980','mm/dd/yyyy') AND TO_DATE ('01/01/2021', 'mm/dd/yyyy') AND rownum<5
SELECT * FROM some_table a where TO_DATE (A.CREATE_DATE, 'dd/mm/yyyy') BETWEEN TO_DATE ('01/01/1980','dd/mm/yyyy') AND TO_DATE ('01/01/2021', 'dd/mm/yyyy') AND rownum<5
SELECT * FROM some_table a WHERE TO_DATE(A.CREATE_DATE,'dd-MON-yyyy') BETWEEN TO_DATE ('01-JAN-1980','dd-MON-yyyy') AND TO_DATE ('01-JAN-2021','dd-MON-yyyy') AND rownum<5
SELECT * FROM some_table a where TO_DATE (A.CREATE_DATE, 'DD/MM/YYYY') BETWEEN TO_DATE ('01/01/1980','DD/MM/YYYY') AND TO_DATE ('01/01/2021', 'DD/MM/YYYY') AND rownum<5
In Oracle DB the date is in this format '15-Sep-2020 10:22:23'
But when run the code from Java using ojdbc7-12.1.0.2.jar driver, I am not getting any results
when I remove where condition (SELECT * FROM acadmin.ctmgmt_call_info a where rownum<5). I am getting results. For testing, I have hard-coded the dates.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("url","username", "pwd");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
System.out.println(rs.getString(1));
}
How to make it work with date where condition?
I was able to fix the issue by replacing TO_DATE (A.CREATE_DATE, 'dd/mm/yyyy') with A.CREATE_DATE
SELECT * FROM some_table A where A.CREATE_DATE BETWEEN TO_DATE ('01/01/1980','dd/mm/yyyy') AND TO_DATE ('01/01/2021', 'dd/mm/yyyy') AND rownum<5

Why won't Postgres filter my date range partitions?

I have a table that uses declarative partitioning (w00t!) to partition tables by date range - one year in my case.
When I query against the table - SELECT * FROM tbl WHERE date > date '2016-01-01', it works exactly as intended; only tables containing newer data are scanned.
When I specify a date using variables or functions (CURRENT_DATE, NOW(), etc), EXPLAIN says it scans every partition.
Things that work as intended:
SELECT * FROM tbl WHERE date > date '2016-01-01'
--
SELECT * FROM tbl WHERE date > '2016-01-01'::date
Things that scan all partitions unnecessarily:
SELECT * FROM tbl WHERE date > CURRENT_DATE
--
SELECT * FROM tbl WHERE date > NOW()
--
SELECT * FROM tbl WHERE date > (NOW() - 365)::date
--
SELECT * FROM tbl WHERE date > (SELECT (NOW()::date - 365)::date AS d)
-- Even CTEs are no dice:
WITH a AS (SELECT CURRENT_DATE AS d)
SELECT * FROM tbl, a WHERE date > a.d
-- Same with JOINs
SELECT w.*
FROM (CURRENT_DATE - 365 as d) a
LEFT JOIN wtf w ON w.date > a.d
..etc
I get the same behavior with other comparison operators - =, <, etc.
The docs say I don't need an idx on the field (which I don't anyways). I added one just in case and it did not help.
Why is this happening, and what can I do to stop it (preferably without adding complication to a simple query)?
Thanks to JustMe for answering this- see the comments on the OP.
The issue lies with when NOW() and CURRENT_TIMESTAMP are evaluated in relation to FROM; it's the same issue you see when you try to filter in a join ala WHERE join_table.a > from_table.b.
Supposing today is Jan 1, 1970, these queries
SELECT * FROM my_stuff WHERE date > NOW()::date;
--
SELECT * FROM my_stuff WHERE date > '1970-01-01'::date;
will necessarily produce an identical resultset but will not necessarily be evaluated in an identical way.
That's why this is happening and unfortunately, there doesn't seem to be a simple way to stop it. A function seems to be the best-ish option:
CREATE OR REPLACE FUNCTION myfunc()
RETURNS setof tbl
LANGUAGE 'plpgsql'
AS $$
DECLARE
n date := CURRENT_DATE - 365;
BEGIN
RETURN query EXECUTE $a$
SELECT * FROM tbl
WHERE date > $1;
$a$ using n;
END $$;
You can test this by changing RETURNS setof tbl to RETURNS setof text and SELECT... to EXPLAIN SELECT...

Using Postgres, I want a timestamp in a specific format in a string in the query

Here is the query I would like
SELECT * FROM information_schema.tables WHERE table_name = 'shop_ean_2016_06_12';
but "2016_06_12" in the query should be yesterday.
I know "NOW() - '1 day'::INTERVAL" but I want to use it in the table name comparison.
Thank you for any way of doing this.
select * from information_schema.tables where table_name ='shop_ean_' || to_char(now() - interval '1 day','YYYY_MM_DD');