postgresql error : Multiple decimal points - postgresql

So I'm having this query:
SELECT
TO_CHAR(date_part('hour', created_at), 'YYYY-MM-DD HH24'),
to_char(created_at, 'day') ",
COUNT(*) AS "
FROM table
GROUP BY 1,2
ORDER BY 1 DESC
When I execute the query I get this:
ERROR: multiple decimal points
Searching stackoverflow I found some recommendations here:
How to format bigint field into a date in Postgresql? but I don't get why do I have to divide by 1000 and how this would apply in the case of the date_part function.

I assume created_at is a timestamp?.. I'm choosing from date_part(text, timestamp) and date_part(text, interval), if so date_part will return a double precision, to which you try to apply the mask 'YYYY-MM-DD HH24', eg:
v=# select date_part('hour', now());
date_part
-----------
9
and I don't see how you could possibly get year, month, day and hour from nine...
Yet I assume you wanted to apply the mask against truncated date to the hour precision, which is done with date_trunc(text, timestamp):
v=# select date_trunc('hour', now());
date_trunc
------------------------
2017-06-20 09:00:00+01
(1 row)
so now you can apply the time format:
v=# select to_char(date_trunc('hour', now()),'YYYY-MM-DD HH24');
to_char
---------------
2017-06-20 09
(1 row)
but if this is what you want, then you don't need to truncate time at all:
v=# select to_char(now(),'YYYY-MM-DD HH24');
to_char
---------------
2017-06-20 09
(1 row)
https://www.postgresql.org/docs/current/static/functions-datetime.html

Related

postgresql - NOW() but less specific

When I use SELECT NOW(); I'll get an output like this:
now
-------------------------------
2019-09-09 18:55:38.794006-05
(1 row)
I want it like this:
now
-------------------
2019-09-09 18:55:38
(1 row)
How do I make NOW() round up/down accordingly? I tried SELECT NOW()::timestamptz(0); but it keeps adding -05 to the end of the time :(
You can to_char
select to_char(now(), 'YYYY-MM-DD HH:MI:SS')
or use date_truc to retain datatype timestamp with time zone
select date_trunc('minute', now());
Convert NOW() to a timestamp without timezone:
SELECT NOW()::timestamp(0)

Convert Julian Date to Timestamp in PostgreSQL

I have a table full of "Julian Dates", that is the number of days and seconds away from 1/1/2035. I need to convert these to normal postgres timestamps. Can anyone help?
--Converts '2000-06-20 12:30:15' into an Epoch time base which gives a result of -12612.478993055556
select (EXTRACT(epoch FROM ('2000-06-20 12:30:15'::timestamp - '2035-01-01 00:00:00'))/86400.00) as run_ts
--Question, how to convert -12612.478993055556 back into '2000-06-20 12:30:15'
select -12612.478993055556 ??? as run_ts
You can use to_timestamp() to convert an epoch to a timestamp.
The epoch you posted does not correspond to 2000-06-20, as you have removed another date 2035-01-01 from it.
select (EXTRACT(epoch FROM ('2000-06-20 12:30:15'::timestamp )));
date_part
-----------
961504215
(1 row)
select to_timestamp(961504215);
to_timestamp
------------------------
2000-06-20 08:30:15-04
(1 row)
select to_timestamp(-12612.478993055556);
to_timestamp
-------------------------------
1969-12-31 15:29:47.521007-05
(1 row)
EDIT
Since you are not considering a true epoch but really a difference between two dates, you can simply add this difference to the reference date. You can use the day interval to remove the need to multiply by 86400 (seconds/day)
select '2035-01-01 00:00:00'::timestamp + interval '1' day * -12612.478993055556;
?column?
---------------------
2000-06-20 12:30:15

Postgresql: add 24 hours on a timestamp

Hi i have to add 24 hours on a timestamp converted from a string in postgres db.
here my code:
select to_timestamp(timestamp_start, 'YYYY-MM-DD HH24:MI:SS.US') + interval '24 hour' as tstamp from tablename
the query works but it adds two 0 at the end of the timestamp: "2017-05-23 17:35:13.105867+00"
why and how to solve it?
+00 meant it is timestamp with timezone and your client timezone is UTC.
If you dont want those +00 on the screen, cast it to timestamp without timezone, eg:
t=# select now();
now
-------------------------------
2017-05-23 09:04:46.105322+00
(1 row)
Time: 0.690 ms
t=# select now()::timestamp;
now
----------------------------
2017-05-23 09:04:51.849522
(1 row)
Time: 0.537 ms
So for query in original post it would be:
select (to_timestamp(timestamp_start, 'YYYY-MM-DD HH24:MI:SS.US') + interval '24 hour')::timestamp as tstamp
from tablename

PGSQL - How do I Add 5 hours to DATE in WHERE Clause?

I either need to add 5 hours or convert from GMT to EST. The return is currently showing everything from 7p and later yesterday...
WHERE
incident.initial_symptom = 'Chrome Upgrade' AND
DATE(incident.install_completed) = CURRENT_DATE;
Instead of manually adding interval to get wanted time zone, use at time zone, eg:
t=# select now(), now() at time zone 'est';
now | timezone
------------------------------+---------------------------
2017-04-07 07:07:39.17234+00 | 2017-04-07 02:07:39.17234
(1 row)
Depending on your timezone, exactly same statement adding interval to your date gives different result, eg at DST shift hour:
t=# set timezone TO 'WET';
SET
t=# select '2017-03-26 00:00:00'::timestamptz + '1 hour'::interval;
?column?
------------------------
2017-03-26 02:00:00+01
(1 row)
t=# set timezone TO 'EET';
SET
t=# select '2017-03-26 00:00:00'::timestamptz + '1 hour'::interval;
?column?
------------------------
2017-03-26 01:00:00+02
(1 row)

Simplify calculation of months between 2 dates (postgresql)

This question is asked many times and one of the suggested queries to get months between 2 dates is not working.
SELECT date_part('month',age('2016-06-30', '2018-06-30'))
The result of this query is 0. It should be 24 months. Because the months are 06 in both dates.
This works, but it is a bit clumsy compared to the sql server function:
SELECT date_part ('year', f) * 12 + date_part ('month', f)
FROM age ('2016-06-30', '2018-06-30') f
Like sql server (I think):
DATEDIFF(month, date1, date2)
Is there no simple way (like the above) to calculate the months between 2 dates in Postgresql? I prefer not to use a function if it is possible.
Unfortunately you already have the most elegant solution.
If you look at the documentation for extract (same as date_part):
https://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
month
For timestamp values, the number of the month within the year (1 - 12) ; for interval values, the number of months, modulo 12 (0 - 11)
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 2
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
Result: 3
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
Result: 1
For your problem it would be nice if there was a version of month that wasn't modulo 12 but that doesn't exist.
The option you have (extract the year * 12 + month) is the best option there is.
Edit
If you do want to create a function then see the following two functions:
CREATE OR REPLACE FUNCTION get_months(i interval) RETURNS double precision AS $$
SELECT date_part ('year', i) * 12 + date_part ('month', i) ;
$$ LANGUAGE SQL IMMUTABLE;
SELECT get_months(age('2016-06-30', '2018-06-30'));
Or
CREATE OR REPLACE FUNCTION get_months(to_date date, from_date date) RETURNS double precision AS $$
SELECT date_part ('year', f) * 12 + date_part ('month', f)
FROM age (to_date, from_date) f;
$$ LANGUAGE SQL IMMUTABLE;
SELECT get_months('2016-06-30', '2018-06-30');
You can actually create both then just use whichever suits your code.
This will give you the # of months between two dates excluding days.
CREATE OR REPLACE FUNCTION get_months_between(to_date date, from_date date) RETURNS double precision AS $$
SELECT (date_part ('year', to_date) * 12 + date_part ('month', to_date)) - (date_part ('year', from_date) * 12 + date_part ('month', from_date))
$$ LANGUAGE SQL IMMUTABLE;