BigQuery Google timezone conversion - google-bigquery

When I try running below query I get timestamp in UTC timezone.
select current_timestamp from table;
Can you please help me to convert timestamp to get in EST timezone.
Thanks

SELECT TIMESTAMP(DATETIME(CURRENT_TIMESTAMP), 'America/New_York')
The trick here is in converting TIMESTAMP to DATETIME which is timezone-less to represent timestamp as a just date/time, then convert back to TIMESTAMP but now specifying needed timezone.
Note, BigQuery still will show it as UTC but timestamp value itself will represent value in respective timezone

Try this instead:
SELECT STRING(CURRENT_TIMESTAMP, 'America/New_York') AS current_timestamp
FROM dataset.table
This converts the timestamps to strings using the New York time zone.

For more control you can use FORMAT_TIMESTAMP (https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#format_timestamp)
Example:
SELECT FORMAT_TIMESTAMP("%F %T EST", current_timestamp, "America/New_York") AS current_timestamp_EST FROM `dataset.table` LIMIT 1;
Results:
|-------------------------|
| current_timestamp_EST |
|-------------------------|
| 2020-10-23 18:25:17 EST |
|-------------------------|

Related

API field configuring

I see timestamp variable in api getting converted to 2 hours ahead and stored what time zone it is?
I want to know what timezone configuration is set to store how to set it for cet time
I will try to explain you how i do it for PsotgreSQL Here is an example that should help. If you have a timestamp with a timezone, you can convert that timestamp into any other timezone. If you haven't got a base timezone it won't be converted correctly.
SELECT now(),
now()::timestamp,
now() AT TIME ZONE 'CST',
now()::timestamp AT TIME ZONE 'CST'
Output:
-[ RECORD 1 ]---------------------------
now | 2018-09-15 17:01:36.399357+03
now | 2018-09-15 17:01:36.399357
timezone | 2018-09-15 08:01:36.399357
timezone | 2018-09-16 02:01:36.399357+03

get time for timezone

I'm hurting my head again this :
On postgresql, I would like to get the local time for a given timezone.
So, at 15:45 GMT, I want 16:45 for +01:00, but I can't get the good anwser :
SQL Fiddle
Query 1:
select current_timestamp at time zone 'GMT' as time_local_gmt
Results:
| time_local_gmt |
|-----------------------------|
| 2018-01-26T15:45:10.871659Z |
This is OK.
Query 2:
select current_timestamp at time zone '+01:00' as time_local_paris
Results:
| time_local_paris |
|-----------------------------|
| 2018-01-26T14:45:10.871659Z |
This is totally wrong, seem like it's -01:00 instead of +01:00
Edit :
See the valid answer here : https://stackoverflow.com/a/48707297/5546267
This worked for me.
select current_timestamp at time zone 'UTC+1';
Gave me the following result.
2018-01-26T17:00:58.773039Z
There is also a list of timezone names.
Here is an excerpt from the PostgreSQL 9.6 documentation regarding timezone names.
The view pg_timezone_names provides a list of time zone names that are recognized by SET TIMEZONE, along with their associated abbreviations, UTC offsets, and daylight-savings status.
Basically, the following query will give you the current time in Paris.
SELECT current_timestamp AT TIME ZONE 'Europe/Paris';
Good Luck!
For completeness (even if #Avi Abrami's answer should be what you're searching for) let's take a look at the datetime operators in the docs.
One can use the INTERVAL keyword to add hours to the stored value:
SELECT current_timestamp AT TIME ZONE INTERVAL '+02:00' AS plus_two;
Which then results in
2018-01-26T17:45:10.871659Z
(when GMT time is 2018-01-26T15:45:10.871659Z)
Section 9.9.3 AT_TIME_ZONE mentions my use of INTERVAL without any preceeding operator:
In these expressions, the desired time zone zone can be specified either as a text string (e.g., 'PST') or as an interval (e.g., INTERVAL '-08:00'). In the text case, a time zone name can be specified in any of the ways described in Section 8.5.3.
The documentation says:
Another issue to keep in mind is that in POSIX time zone names, positive offsets are used for locations west of Greenwich. Everywhere else, PostgreSQL follows the ISO-8601 convention that positive timezone offsets are east of Greenwich.
I guess that is your problem.
Ok, finally found how to !
SELECT
current_timestamp
AT TIME ZONE 'GMT'
AT TIME ZONE '+01:00'
AS time_local_paris_right;
The timestamp is UTC without TZ by default, you force it as a GMT one, and then the second AT convert it with the right offset to give you the local time for the specified time zone.
SQL Fiddle
PostgreSQL 9.6 Schema Setup:
Query 2:
select current_timestamp at time zone 'GMT' as time_local_gmt
Results:
| time_local_gmt |
|-----------------------------|
| 2018-02-09T13:44:56.824107Z |
Query 3:
select current_timestamp at time zone '+01:00' as time_local_paris_wrong
Results:
| time_local_paris_wrong |
|-----------------------------|
| 2018-02-09T12:44:56.824107Z |
Query 4:
select current_timestamp at time zone 'GMT' at time zone '+01:00' as time_local_paris_right
Results:
| time_local_paris_right |
|-----------------------------|
| 2018-02-09T14:44:56.824107Z |

Converting string to date with to_date()

I have a table with a VARCHAR(64) column called datetimestamp that contains datetime strings with the following format:
[02/Jun/2016:23:58:30 +0000].
I'm trying to convert this to a date using to_date(datetimestamp, 'DD/Mon/YYYY:HH24:MM:SS') in my select statement, but I'm getting an 'Invalid Format' error. Not sure if its the UTC bit or what that's messing it up... what's the proper syntax?
Thanks!
It is a bit complicated since to_timestamp does not allow time zone information.
I have come up with this query:
WITH d(part) AS
(SELECT regexp_matches(
'02/Jun/2016:23:58:30 +0000',
'^([^ ]*) ([-+]?\d\d)(\d\d)$'
)
)
SELECT
CAST (to_timestamp(d.part[1], 'DD/Mon/YYYY:HH24:MI:SS')
AT TIME ZONE (d.part[2] || ':' || d.part[3])
AS timestamp with time zone)
AS converted
FROM d;
converted
------------------------
2016-06-02 21:58:30+02
(1 row)
(I am at time zone UTC+02.)
select to_date('02/Jun/2016:23:58:30 +0000', 'DD/Mon/YYYY:HH24:MI:SS');
| to_date |
|------------|
| 2016-06-02 |

Extract date,month,year and month name from the unix timestamp with postgresql

I use postgres for the rails app and I have a unix timestamp in postgresql db. I have a requirement to select and group by the dd-mm-yyyy and by month name.
Consider I have the following unix timestamp
1425148200
and I would need to change this to datetime and I used to_timestamp which returned
2015-02-28 18:30:00 UTC
and I tried to convert the datetime to local timezone using
::timestamp without time zone AT TIME ZONE 'IST'
but that did not give time in required timezone and instead it returned
2015-02-28 16:30:00 UTC
and I tried to get the date part using ::date which returned
Sat, 28 Feb 2015
So please help me get the dd-mm-yyyy in specified timezone and month name(March) from the unix timestamp.
Thanks in Advance!
select to_char(to_timestamp('1425148200')::timestamptz at time zone 'UTC-5:30','DD-MM-YYYY & of course Month')
01-03-2015 & of course March
It is postgres mistake I guess
according to http://www.postgresql.org/docs/7.2/static/timezones.html

Converting UTC time field from a POSTGRE/SQL database in SQL

I am having a problem from my database.
I never been used to work in PostgreSQL, and i would like to make a select from a UTC datetime field and getting a GMT Datetime as result.
Actually my request is : select dateheure from position
What should be the Postgresql request to do what i want to do ???
Thanks a lot
Gwenael
PostgreSQL does have two datetime types:
timestamp without time zone (default implicit timestamp)
timestamp with time zone
I guess that you have table with UTC datetime (without time zone type):
CREATE TEMP TABLE datetimetest
(
datetime timestamp
);
\d datetimetest
Table "pg_temp_1.datetimetest"
Column | Type | Modifiers
----------+-----------------------------+-----------
datetime | timestamp without time zone |
INSERT INTO datetimetest SELECT CURRENT_TIMESTAMP AT TIME ZONE 'UTC';
SELECT datetime FROM datetimetest;
datetime
----------------------------
2011-08-15 15:04:06.507166
(1 row)
To get datetime in some timezone you could use AT TIME ZONE construct:
SET TIME ZONE 'UTC';
SELECT datetime AT TIME ZONE 'GMT-5' FROM datetimetest;
timezone
-------------------------------
2011-08-15 10:04:06.507166+00
(1 row)
In a different post I use a CHECK() constraint to make sure that you only store and receive UTC out of the database. Hopefully it's helpful.