Postgres add values in timestamp - postgresql

How could I go about a query that where all parts of the timestamp excluding the year add up to 50,
for example in my database testdb I have my column with a string timestamp, in that column I have my timestamp strings for example,
2016-03-01 07:13:21
2019-07-03 04:04:49
etc so they are separated by the -
so when I query my database, Select timestamp from testdb where.... = 50 I want to see where the individual timestamp strings from all the rows that add up to 50

remove the year, and then add up the day,month,hour,minute and the second and then if that amounts to 50 it will just display those rows that meet the criteria
So you first need to convert your string to a timestamp using e.g. to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
From there you can extract each part of the timestamp using extract:
select *
from the_table
where extract(month from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
+ extract(day from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
+ extract(hour from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
+ extract(minute from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
+ extract(second from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss') = 50
It's a really bad idea to store timestamp values in text (or varchar) columns. They should be stored in columns defined as timestamp or better timestamptz

Related

Create date column from year and doy column

Is there a way to create a date column combining one column having the year as string and one column containing a date-of-year (doy) as integer?
I am aware of methods like SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'); or SELECT to_char(date_trunc('year', now()) + interval '169 days', 'MM/DD') but when trying to replace the "hard coded" stings with the columns I always get some kind of an error.
SELECT s.id, s.year, s.doy,
((s.year||'-01-01')::date + (s.doy||' days')::interval )::date AS date
FROM table_name AS s
the (s.year||'-01-01') or (s.doy||' days') concats the column value with a required string and the ::date or ::interval changes the resulting string type
You can use the make_date() function and add the number of days directly because date + integer is a valid operation:
select make_date(s.year, 1, 1) + s.doy as date
from ...

Oracle to PostgreSQL round and trunc on date columns

Can you please help me to convert the below Oracle query to PostgreSQL query?. Looks like "round" and "trunc" functions are not there in postgreSQL.
Here "intime" is the date and time column i.e date datatype.
select round(((intime - trunc(CURRENT_DATE-1, 'DDD')) * 24),2) from tpe
Below is the posgreSQL format and o/p -2496 days -132:00:00
SELECT (('2021-02-19 08:00:00'::timestamp AT time zone 'EST' - CURRENT_DATE - (interval '1 day'))*24)
Below is ORACLE format and o/p -2464
select ((to_date('02/19/2021 8:00:00','MM/DD/YYYY HH24:MI:SS') - trunc(CURRENT_DATE-1, 'DDD')) * 24) from dual;
And When apply round function in postgreSQL it's throwing error
SELECT round((('2021-02-19 08:00:00'::timestamp AT time zone 'EST' - CURRENT_DATE - (interval '1 day'))*24),2)
"ERROR: function round(interval, integer) does not exist.
Idea is to get same output of oracle's one.

Hive queries with dates

I have a little problem, I would like to filter a date with hive query but the output is empty. My column is string type
I tried this :
select * from my_table
where to_date(date) < to_date('01/08/19 00:00:00')
The format of my column date is 01/08/19 18:00:00
Dates in this format '01/08/19 00:00:00' are not in comparable format because in such format '02/08/19 00:00:00' is greater than '01/08/20 00:00:00'.
Use unix_timestamp and from_unixtime to convert to the comparable format ('yyyy-MM-dd HH:mm:ss'), then compare with date in the same format.
select * from my_table
where from_unixtime(unix_timestamp(date,'dd/MM/yy HH:mm:ss'),'yyyy-MM-dd HH:mm:ss') < '2019-08-01 00:00:00'

How define today date with Default timestmp

I am using postgressql i wish to get the data for currentdate, i want filter the data based on the date
in data base my plandate filed is define as Time stamp with time zone so its showing like this format 2013-09-01 03:22:01.438348+05:30 my query is like this
select ttodoid ,date,details from ttodo where date=currentdate():
but currentdate function giving me just date '2013-10-06' based on this result is no rows how can i manage it for today date detail
UPDATED: One way to do it
SELECT *
FROM ttodo
WHERE date BETWEEN DATE_TRUNC('day', CURRENT_TIMESTAMP)
AND DATE_TRUNC('day', CURRENT_TIMESTAMP)
+ INTERVAL '1 DAY'
- INTERVAL '1 MICROSECOND';
or
SELECT *
FROM ttodo
WHERE date >= DATE_TRUNC('day', CURRENT_TIMESTAMP)
AND date < DATE_TRUNC('day', CURRENT_TIMESTAMP)
+ INTERVAL '1 DAY';
Here is SQLFiddle demo
select * from ttodo where (ttodo.todoplandate::date = current_date) or
(ttodo.todoplandate::date < current_date
I think the easier approach would be just to convert your field to date:
SELECT ttodoid ,date,details FROM ttodo
WHERE CAST(date AS DATE) = current_date;
Notice that, ff you want this query to be indexed, you have to create the index with the cast:
CREATE INDEX idx_ttodo_date ON ttodo ((CAST(date AS DATE)));
Another approach, is instead of casting the field, is checking the intervals, something similar of what petern proposed, but with correct intervals:
SELECT ttodoid ,date,details FROM ttodo
WHERE date >= date_trunc('day', current_timestamp)
AND date < (date_trunc('day', current_timestamp) + interval '1day');
This approach has the advantage that it can use an index on the date field only, which is good if you already have it.

PgSQL turning day-of-year back into date

I am trying to determine how to turn a day-of-year back into a date in PgSQL. When I do this
select date '2013-01-01' + interval '53 days'
I get a timestamp:
"2013-02-23 00:00:00"
So how come when I do any of the following
select extract(date from (date '2013-01-01' + interval '53 days'))
select extract(date from (select date '2013-01-01' + interval '53 days'))
I get "ERROR: timestamp units "date" not recognized"? Besides the why, how can I do what I want, which is to only get the date portion of the result of the original operation?
Use
select (date '2013-01-01' + interval '53 days')::date
or
select cast(date '2013-01-01' + interval '53 days' as date)
PostgreSQL's standard SQL function "extract()" will operate on timestamps, but a) "date" isn't a valid argument to extract(), and b) it returns subfields, not a collection of subfields. Conceptually, a date consists of a collection of three subfields: year, month, and day.
select extract(year from current_timestamp),
extract(month from current_timestamp),
extract(day from current_timestamp),
-- Concatenate and cast to type "date".
(extract(year from current_timestamp) || '-' ||
extract(month from current_timestamp) || '-' ||
extract(day from current_timestamp))::date