Convert DATETIME format to custom format in bigquery - sql

I have a column in BigQuery in DATETIME eg 2018-08-16T11:00:35.683000 which I would like to convert in some custom format which is YYYY-MM-dd HH:MM.
How can I do this ?
I am getting below error while converting:
ValueError: timestamp out of range for platform localtime()/gmtime() function

Below example for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT DATETIME '2018-08-16T11:00:35.683000' dt
)
SELECT FORMAT_DATETIME('%Y-%m-%d %R', dt) cust_dt
FROM `project.dataset.table`
with result
Row cust_dt
1 2018-08-16 11:00

Related

Converting Abbreviated Date to Date

I have a truncated "date" attribute in the dataset, and need to convert the value below to a full DATE format (assuming it's the current year).
SELECT '6-May'
would output:
2020-05-06
SQL Server is very flexible about recognizing date formats. If you want to produce a date datatype, you can cast as follows:
select cast(mycol + '-2020' as date) mydate from mytable

AWS Athena (Presto) - how to format Timestamp to Date Format?

I have a column in Athena with Timestamp Data Type and format is: 2019-08-28 00:00:00.000
How to format it to Date format using SQL to be:
DD-MON-YYYY
Thanks.
WITH test AS (
SELECT '2019-08-28 00:00:00.000' AS str
)
SELECT format_datetime(cast(str AS timestamp), 'dd-MM-YYYY')
FROM test
Result:
_col0
1 28-08-2019

BigQuery convert String to Date

In my dataset, one column called timestamp was created with datatype as String.
It contains values like:
2018-05-30T12:56:27:487+0200
I want to construct a query where I can fetch all column from the dataset table based on the date in 'YYYY-MM-DD' format.
I want to use it in where clause with DATE Range between.
Can you guide?
Thank you.
convert String to Date
Below example for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT '2018-05-30T12:56:27.487+0200' ts UNION ALL
SELECT '2018-05-30T01:56:27.487+0200'
)
SELECT ts AS ts_as_string,
PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts) ts_as_timestamp,
DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts)) ts_as_date
FROM `project.dataset.table`
with result
ts_as_string ts_as_timestamp ts_as_date
2018-05-30T12:56:27.487+0200 2018-05-30 10:56:27.487 UTC 2018-05-30
2018-05-30T01:56:27.487+0200 2018-05-29 23:56:27.487 UTC 2018-05-29
As you can see - first i am parsing timestamp out of the string - this is important part - this is where you can take timezone into account (see difference in dates in the result 2018-05-29 vs. 2018-05-29). Then you can get Date out of TIMESTAMP
I want to use it in where clause with DATE Range between.
So, now you can use below in your WHERE clause
WHERE DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts)) BETWEEN date1 AND date2
Update
You can use below to avoid dealing with "wrong" format
PARSE_DATE('%F', SUBSTR(ts, 1, 10))
In case if you need to account for timezone - you can use below (which fix : to . before applying PARSE_TIMESTAMP)
DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', FORMAT('%s.%s', SUBSTR(ts, 1, 19), SUBSTR(ts, 21, 8))))
If you want the date in the same timezone represented, then the simplest method is to use string operations and convert to a date:
select PARSE_DATE('%Y-%m-%d', SUBSTR('2018-05-30T12:56:27:487+0200', 1, 10))

How to convert the date July 1, 2017 to dd-MM-yyyy using Hive SQL?

I have a Hive table with a Week column having values such as:
I have to convert this field to a date format such as: 2017-07-01 (yyyy-MM-dd) using hive SQL.
Any suggestions?
You can use a combination of from_unixtime and unix_timestamp.
select from_unixtime(unix_timestamp(weekCol,'MMM dd, yyyy'),'yyyy-MM-dd')
Use a combination of unix_timestamp and from_unixtime
select from_unixtime(unix_timestamp(week,'MMMM dd, yyyy'),'yyyy-MM-dd') from table_name;
unix_timestamp(string datetime, string pattern) converts datetime with given pattern to unix time stamp.
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch.

Create a view in bigquery with date rounded down to midnight?

I am creating a view in BigQuery with the following SQL statement:
SELECT Id, CreatedDate FROM [bucketname.tablename]
How can I modify this query so that the CreatedDate becomes rounded down to midnight in the view?
in BigQuery legacy SQL: try TIMESTAMP(DATE(CreatedDate))
as an example
CreatedDate = '2016-04-30 07:01:28 UTC'
DATE(CreatedDate) = '2016-04-30'
and finally TIMESTAMP(DATE(CreatedDate)) = '2016-04-30 00:00:00 UTC'
In mode of standart SQL: try Format functions
FORMAT_TIMESTAMP("%Y-%m-%d 00:00:00", CURRENT_TIMESTAMP())
I believe you can just use the DATE function:
Returns a human-readable string of a TIMESTAMP data type in the format %Y-%m-%d.
So:
SELECT Id, DATE(CreatedDate) AS CreatedDate From [dataset.tablename]