6

I'm finding in Postgresql and I want to remove +9 UTC value in my query.

For example: In to_timestamp column, I want to remove +09, and only keep 2016-02-26 00:23:44 value.

This is my query:

select t,to_timestamp(v) from c1.hour where f = 22 and date_trunc('day',t - '1 hour'::INTERVAL) = '2016-02-26 00:00:00' order by t ;

Here my result: enter image description here

And this is my result when I didn't use to_timestamp: enter image description here

Can I get some help on this please?

1
  • 1
    Start by providing your table definition - data types are essential to your question. And never post code as image. We very much prefer text for that. Commented Feb 26, 2016 at 3:37

3 Answers 3

17

Use to_char() function to format timestamp to any desired string representation. Example:

SELECT to_char(now(), 'YYYY-MM-DD HH24:MI:SS')

will return 2016-02-26 09:37:49 (without a timezone)

Your query have to be this:

SELECT t, to_char(to_timestamp(v), 'YYYY-MM-DD HH24:MI:SS') 
FROM c1.hour 
WHERE 
   f = 22 
   AND date_trunc('day',t - '1 hour'::INTERVAL) = '2016-02-26 00:00:00' 
ORDER BY t ;
Sign up to request clarification or add additional context in comments.

Comments

6

Specify UTC:

select to_timestamp(0)::timestamptz at time zone 'UTC'

I am in timezone +08 and get the following result:

1970-01-01 00:00:00

With the time zone specification, I get:

1970-01-01 08:00:00+08

An advantage is you don't have to specify the date-time format, which can cause unexpected results for users. The system specified formatting is preserved.

3 Comments

I believe, OP wanted 1970-01-01 08:00:00 instead.
Yes, that's right. at time zone "UTC" will do that.
at time zone "UTC" subtracts 8 hours as well, that seems to be not what OP wants, but just to remove +08 in your case while keeping the rest intact as if at your zone.
4

You can do this: cast("your column" as timestamp)

2 Comments

Very useful approach. I ended up doing trunc('minute', cast (col_timestand_with_tz as timestamp))
that seems like a simple solutijon but not tested whether it is to simple and only looks right because i am either GMT or BST so +00 or +01 so difference would be minimal. the answers that are more specific and specify 'at time zone "UTC"' seem more specific

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.