Is there a better way to get local time to the second in PostgreSQL than this?
Set timezone= 'America/New_York';
Select to_char(current_timestamp, 'MM/DD/YYYY HH24:MI:SS') As EasternCurrentTime;
We do a lot of these in different places for logging within our ETL and would like to do it on one line.
I'd suggest
SELECT to_char(current_timestamp AT TIME ZONE 'America/New_York',
'MM/DD/YYYY HH24:MI:SS');
Related
I have trying to convert the UTC time to local time in Oracle Developer as per query below. I needed it in a particular format after conversion but after conversion to character the time comes out to be completely different. Can anyone tell me what am I doing wrong here please.
select e.encntr_id
,to_char(e.reg_dt_tm,'YYYY-MM-DD hh24:mm') as reg_dt_tm
,to_char(from_tz (cast(e.reg_dt_tm as timestamp),'UTC') at time zone
'Australia/Sydney','YYYY-MM-DD hh24:mm') as aest_reg_char
,from_tz (cast(e.reg_dt_tm as timestamp),'UTC') at time zone 'Australia/Sydney' as aest_reg
from encounter e
where e.ENCNTR_ID in(123)
encntr_id reg_dt_tm aest_reg_char aest_reg
123 2022-03-03 05:03 2022-03-03 16:03 03/MAR/22 04:51:12.000000000 PM AUSTRALIA/SYDNEY
Use TO_CHAR:
TO_CHAR(
from_tz (cast(e.reg_dt_tm as timestamp),'UTC') at time zone 'Australia/Sydney',
'YYYY-MM-DD hh24:mm:ss.ff TZR'
)
Or change the default TIMESTAMP_TZ format in SQL Developer:
ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF TZR';
and then run the query.
db<>fiddle here
I have a postgres database carrying date/time information in a text format. There is no way of changing it, but I have to retrieve those values as milisecons since epoch.
I managed to make a query, converting those date-time records to timestamps so that I get a correct "max" function behaviour like so:
SELECT max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name;
But converting other results into miliseconds does not seem to work. And all the examples int the documentation and forums showcase only the usage for some literal value, not a value selected from a database. So lines like these don't work:
SELECT EXTRACT(EPOCH FROM TIMESTAMP
(select max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP
(select TO_TIMESTAMP(column_name,'YYYY/MM/DD HH24:MI:SS')
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE(
SELECT TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS')
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE TO_TIMESTAMP
(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name;
Is there an actual way to accomplish what I want by using a query, or I have to do something more complicated?
P.S.
Of course I can just retrieve all the infomation as text and use Qt (QDateTime) to convert it to miliseconds, but It would be more expensive and I was wondering if there is a way to ask the database to do it for me.
The timestamp keyword is only needed for literals (constants), not if you have a proper timestamp value available:
SELECT extract(epoch from max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS')))
FROM table_name;
Note that epoch represents seconds, not milliseconds.
This is really stumping me and it doesn't seem like it should be that difficult, but in postgres 9.6, I'm trying to format a timestamp with the offset.
Here's the closest I've gotten:
SELECT to_char('2017-11-06 00:00:00'::TIMESTAMP WITH TIME ZONE AT TIME ZONE 'America/Vancouver', 'MM/DD/YYYY HH24:MI:SS (OF)');
The above example gets the right date, but the offset is +00, which is incorrect.
Any ideas?
EDIT:
Additionally, how do I set this in a function? The following doesn't work:
DECLARE
_tz text = 'PST8PDT';
BEGIN
SET LOCAL TIME ZONE _tz;
...
First you set the time zone of the session, then you set the timezone that the timestamp is in. Like so:
SET TIME ZONE 'America/Vancouver';
SELECT to_char('2017-11-06 00:00:00'::TIMESTAMP AT TIME ZONE 'UTC', 'MM/DD/YYYY HH24:MI:SS (OF)');
I have TableA and it has a field of
time_captured | timestamp without time zone | default now()
It is being used to record when data was inserted into the table. However, I am trying to do a select and ignore milliseconds.
So instead of having
2015-07-11 18:06:33.690498
2015-07-12 13:36:21.274509
2015-07-12 13:36:24.222577
2015-07-12 13:36:26.515593
I would like to have
2015-07-11 18:06:33
2015-07-12 13:36:21
2015-07-12 13:36:24
2015-07-12 13:36:26
Having the microseconds in the table is needed but what I do not need is for the output to show the microseconds.
How can I do this ?
The closest I have come is by using the following but I am not able to figure out how to pull the full date with the time as shown above.
SELECT EXTRACT(YEAR FROM time_captured AT TIME ZONE 'UTC') FROM server_perf; date_part
Thank you :)
You can use the date_trunc function to achieve this:
select date_trunc('second', time_captured) from server_perf;
see http://www.postgresql.org/docs/9.1/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC for more on this.
I have a column added_at of type timestamp without time zone. I want it's default value to be the current date-time but without time zone. The function now() returns a timezone as well.
How do I solve that problem?
SELECT now()::timestamp;
The cast converts the timestamptz returned by now() to the corresponding timestamp in your time zone - defined by the timezone setting of the session. That's also how the standard SQL function LOCALTIMESTAMP is implemented in Postgres.
If you don't operate in multiple time zones, that works just fine. Else switch to timestamptz for added_at. The difference?
Ignoring time zones altogether in Rails and PostgreSQL
BTW, this does exactly the same, just more noisy and expensive:
SELECT now() AT TIME ZONE current_setting('timezone');
Well you can do something like:
SELECT now() AT TIME ZONE current_setting('TimeZone');
SELECT now() AT TIME ZONE 'Europe/Paris';
SELECT now() AT TIME ZONE 'UTC';
Not sure how that makes any sense for a column "added_at". You almost always want an absolute timestamp (timestamp with time zone) not a floating one.
Edit responding to points below:
Yes, should use timestamp with time zone (absolute time) unless you have a good reason not to.
The client timezone is given by SHOW TimeZone or current_setting(...) as shown above.
Do take some time to skim the manuals - they cover all this quite well.
"Current Date/Time":
CURRENT_TIME and CURRENT_TIMESTAMP deliver values with time zone; LOCALTIME and LOCALTIMESTAMP deliver values without time zone.
New, and Native Answer in 2020
In PostgreSQL, If you only want the current date-time by calling CURRENT_TIMESTAMP() without time zone, and fractional digits in the seconds field which come after the decimal point of the seconds field?
(Tested on PostgreSQL v12.4)
Then use this:
SELECT CURRENT_TIMESTAMP(0)::TIMESTAMP WITHOUT TIME ZONE;
If you define your column's data type as timestamp (not as timestamptz), then you can store the timestamp without time zone, in that case you don't neet to add TIMESTAMP WITHOUT TIME ZONE
Like this:
CREATE TABLE foo (created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP(0))
In the above function, 0 is passed to get rid of the fractional digits in the seconds field.
If your application doesn't care about timezone, you can use SELECT LOCALTIMESTAMP for it.
Ex:
SELECT LOCALTIMESTAMP
-- Result: 2023-01-30 17:43:33.628952