Convert cst to pst in big query - google-bigquery

I have a datetime in a google data base that is in cst. I would like to convert it to pst.
Queries such as
SELECT DATETIME("2008-12-25 15:30:00+07","America/Los_Angeles") as date;
seem to assume that the starting date is in UTC. How can I convert if the starting date is in cst?

You can convert time in Bigquery regardless of what your starting date to a DATETIME in a specific timezone:
SELECT DATETIME("2008-12-25 15:30:00-06" ,"PST8PDT") as date;
Output:

Related

Timestamp string conversion / from_utc_timestamp

I need to convert 2021-10-03 15:10:00.0 as 2021-10-03T15:10:00-04:00
I tried with.
from_utc_timestamp(from_unixtime(unix_timestamp('2021-10-03 15:10:00.0', "yyyy-MM-dd HH:mm:ss.S"),"yyyy-MM-dd'T'HH:mm:ssXXX"),"America/New_York")
I got Null value
Any suggestions please
from_utc_timestamp can accept timestamp or compatible string (yyyy-MM-dd HH:mm:ss.S), or bigint, not this: "yyyy-MM-dd'T'HH:mm:ssXXX"
Hive timestamps are timezoneless. Once you converted from UTC to America/NY, the timezone information is lost, only you know in which timezone it is, having timestamp converted it is already impossible to derive the timezone from it.
You can concatenate with timezone, conversion like this returns what you need but it works for particular date only. In December -05:00 timezone should be usedm not +04:00:
date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss+04:00") --This is wrong!!!
From_utc_timestamp is Daylight saving aware. It can be -05:00 or -04:00 depending on the date.
Consider this example, first returns 5, second returns 4:
select (unix_timestamp("2020-01-01 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-01-01 12:00:00.0","America/New_York")))/60/60
select (unix_timestamp("2020-10-19 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-10-19 12:00:00.0","America/New_York")))/60/60
So, you can get current time zone corresponding to America/New_York for the same timestamp and concatenate it with converted timestamp:
select concat(date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss"),'+0',
--get hrs shift
(unix_timestamp("2021-10-03 15:10:00.0")-unix_timestamp(from_utc_timestamp("2021-10-03 15:10:00.0","America/New_York"))) div 3600,':00')
Result:
2021-10-03T11:10:00+04:00
It should work correctly with different timestamps taking into account daylight saving time for America/New_York.

BigQuery: Convert DateTime in different time zone to UTC

I'm new to Big Query,
I want to convert the following date-time to UTC timezone.
DateTime format "2021-07-03T23:59:00+04:00[Asia/Dubai]" to UTC.
Thank you.
You just need to convert it to TIMESTAMP() without specifying any timezone (since it automatically sets the datetime at UTC) and then convert it back to DATETIME():
SELECT
your_datetime,
DATETIME(TIMESTAMP(your_datetime)) AS datetime_utc
FROM
<your_table>
OUTPUT:
your_datetime datetime_utc
------------------------- -------------------
2021-07-03T23:59:00+04:00 2021-07-03T19:59:00

Oracle SQL Select Current Timestamp without Timezone and 24hr Format

I have a Oracle SQL statement where I have to get the current timestamp as one of the columns. But I dont require the Timezone which CURRENT_TIMESTAMP gives or the AM/PM given by LOCALTIMESTAMP.
I require the current timestamp in 24hr format without the timezone.
Is it possible to get that in Oracle SQL?
It seems you're mixing 2 concepts here: "datatype" and "date format mask".
data type: LOCALTIMESTAMP returns datatype TIMESTAMP and CURRENT_TIMESTAMP returns datatype TIMESTAMP WITH TIME ZONE. TIMESTAMP is similar to DATE but has a higher precision. As usual... checking the docs is worth it.
date format mask: determines how you display the date information. Americans can't read 24 hour format, the rest of the world is confused by AM/PM. Fortunately, you can decide how you want to display the date as explained in the oracle docs.
If you just want to return the current date in 24 hour format you could do something like:
SELECT
TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS') as mydate,
<other columns>
FROM
<table_name>
If you need the date to be more precise and you require fractional seconds then you can use SYSTIMESTAMP instead of DATE with a format mask 'DD-MON-YYYY HH24:MI:SS.FF9'

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

SQLite Data Time Function

I currently have a timestamp in this format Tue Jun 03 17:17:05 +0000 2014 in one column in my table. I want to count the number of records happening in specific intervals (15 minutes). I have tried to follow the answer found in Group records by time. Although my timestamp is in a different format and I haven't seen any support function available in SQLite to convert this. Is this possible in SQL?
The SQLite date and time functions can be used to convert a timestring to a canonical format, or to a Julian Day Number. Unfortunately, the SQLite date and time functions only accept timestring in a limited number of formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
If your timestring format has fixed field widths, you can use the substr function and the || string concatenation operator to convert it to a format SQLite understands. You'll have to use a case expression to convert the month names to numbers; here's an example.
You may use NEW_TIME in Oracle to convert the time to a specific timezone. Here is an example. This example is converting SYSDATE from PDT to GMT.
SELECT NEW_TIME (SYSDATE, 'PDT', 'GMT') FROM DUAL;
This thread is detailing how to add required minutes to your timestamp.