Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting where timestamp greater than with timezone

Tags:

sql

postgresql

Suppose I have this table structure:

                          Table "test"
     Column     |            Type             | Modifiers
----------------+-----------------------------+-----------
 uuid           | uuid                        | not null
 created        | timestamp without time zone | not null

How would I select records after a certain date? But also factor in a specific timezone?

Since created is timestamp without zone, we can assume it's UTC

Example query:

select uuid from test where created >= '2017-07-20'

This would return all events that happend on, or after 2017-07-20 00:00:00.000 UTC How would I query for events that happend after say, 2017-07-20 00:00:00.000 GMT+2 ? Without having to add hours to my argument in created > arg

like image 420
Robin Jonsson Avatar asked Jul 20 '17 11:07

Robin Jonsson


People also ask

How do you timestamp with a time zone?

For timestamp with time zone , the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT ). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone.

What is the difference between timestamp with timezone and timestamp without timezone?

With a time zone as part of the value, the value can be rendered as a local time in the client. Without a time zone as part of the value, the obvious default time zone is UTC, so it is rendered for that time zone.

Should I use timestamp with or without timezone?

Instant anyway so timestamp without timezone is actually the preferred choice because even if someone is changing timezones on database server it will still return the correct unchanged UTC.

Does timestamp include timezone?

The definition of UNIX timestamp is time zone independent. The UNIX timestamp is the number of seconds (or milliseconds) elapsed since an absolute point in time, midnight of Jan 1 1970 in UTC time. (UTC is Greenwich Mean Time without Daylight Savings time adjustments.)


1 Answers

select uuid
from test
where created > '2017-07-20'::timestamp with time zone at time zone '+02';
like image 200
Clodoaldo Neto Avatar answered Sep 20 '22 01:09

Clodoaldo Neto