I convert the function, CAST(CAST(Column1 AS CHAR(7)) AS DATE FORMAT 'YYYYDDD') from Teradata to BigQuery as FORMAT_DATE('%E4Y%j',PARSE_DATE('%E4Y%j',CAST(Column1 AS STRING))) where Column1 is DECIMAL in TD so NUMERIC in BQ. If Column1 has value '2020280' in BQ, I get 2020001 in parse results but I need it to be '2020280'. Where do I go wrong ?
PARSE_DATE does not support %j. Use DATE_ADD as a workaround:
select FORMAT_DATE(
'%E4Y%j',
DATE_ADD(
PARSE_DATE('%E4Y', LEFT('2020280', 4)),
INTERVAL (CAST(RIGHT('2020280', 3) AS INT64) - 1) DAY
)
);
Related
I am trying to figure out how to convert and format a BIGINT field (i.e. 20200301) to a DATE type field using Redshift SQL. I was successful in getting the snippet below to work but I believe that returns a string and I need a valid date returned in 'YYYY-MM-DD' format. I've tried several other version unsuccessfully. Thank you in advance.
'''to_char(to_date(date_column::text, 'yyyymmdd'), 'yyyy-mm-dd')'''
You just want the to_date() part:
select to_date(date_column::text, 'YYYYMMDD')
When it is a timestamp we need the below code to convert into correct value.
select trunc(TIMESTAMP 'epoch' + date_column / 1000 * INTERVAL '1 second')
Maturity column is of date datatype.
select TO_char(maturity,'YYYY-MM') || '-15' from tablename
The above query returns me the column value with text datatype. But how can i return the column value as date datatype.
You can cast to date using ::date. ie:
select (TO_char(maturity,'YYYY-MM') || '-15')::date from myTable
You could also do this using date arithmetic:
select maturity + (15 - extract(day from maturity)) * interval '1 day'
In general, I prefer not to convert to strings to do things that can be handled by date/time functions.
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 a date in format YYYY-MM-DD into integer YYYYMMDD in Presto/Hive?
I am trying to convert the below list into YYYYMMDD integers
WITH all_dates as (SELECT
CAST(date_column AS DATE) date_column
FROM
(VALUES
(SEQUENCE(FROM_ISO8601_DATE('2017-07-01'),
FROM_ISO8601_DATE('2017-11-15'),
INTERVAL '1' DAY)
)
) AS t1(date_array)
CROSS JOIN
UNNEST(date_array) AS t2(date_column)
)
I tried something like this but it doesn't work
SELECT
CAST(
CAST(year(date_column) AS VARCHAR(4)) +
right('0' + CAST(month(date_column) AS VARCHAR(2)), 2) +
right('0' + CAST(day(date_column) AS VARCHAR(2)), 2)
AS DATETIME)
FROM all_dates
Also you can use date_format function:
hive> select cast(date_format('2017-07-01','yyyyMMdd') as int);
OK
20170701
If you just need to transform your date YYYY-MM-DD into an integer YYYYMMDD why don't you try to first remove all the occurrences of "-" from the string representation of your date before casting the result to int by using something like this?
cast(regexp_replace(str_column,'-','') as int)
Simply REPLACE the '-' with Empty string and CAST it into INT.
Try the following:
SELECT CAST(REPLACE(Date_Column,'-','') AS INT)
Are you sure you want to use YYYYMMDD ?
Hive follows Java convention for Date Formatting and as per https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
you can see that YYYY represents week year which means you may get into trouble for end of the year dates. 2019 may appear as 2020.
Use yyyyMMdd instead.
#leftjoin has probably the correct respone.
beeline> select cast(date_format('2019-07-01','yyyyMMdd') as int);
OK
20190701
I have input from Amazon Alexa in the format of ISO 8601 and was wandering if I needed to do a whole bunch of string substrings & transforms to make it into a BigQuery Timestamp format, or is there some function that does it?
I also understand that it is hard to turn 2015-W49 into a date, but thought I would ask.
references:
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/built-in-intent-ref/slot-type-reference#date
https://en.wikipedia.org/wiki/ISO_8601#Dates
https://code.google.com/p/google-bigquery/issues/detail?id=208
This can be done via the function PARSE_DATE as detailed here: https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#parse_date
In BigQuery Standard SQL:
SELECT DATE_ADD(PARSE_DATE('%Y', SUBSTR('2015-W49',0,4)), INTERVAL CAST(SUBSTR('2015-W49',7,2) as INT64)-1 WEEK) as parsed;
I would expect below result of such conversion, which is first day of the respective week, which should be respectively :
Week Date in ISO 8601 First Day of the week
2015-W01 2014-12-28
2015-W02 2015-01-04
2015-W49 2015-11-29
You can verify above at http://www.enpicbcmed.eu/calendar/2015-W01 for example
I think below returns correct result
#standardSQL
WITH data AS (
SELECT '2015-W01' AS dt UNION ALL
SELECT '2015-W02' AS dt UNION ALL
SELECT '2015-W49' AS dt
)
SELECT
dt,
DATE_ADD(PARSE_DATE("%Y-W%W", dt),
INTERVAL 7 * CAST(SUBSTR(dt,-2,2) AS INT64) - 6 -
EXTRACT (DAYOFWEEK FROM PARSE_DATE("%Y-W%W", dt)) DAY
) as d
FROM data
ORDER BY dt
This worked for me:
DATE_ADD(
DATE_TRUNC(PARSE_DATE('%Y-%m-%d', CAST(date_close AS STRING)), ISOYEAR),
INTERVAL EXTRACT(WEEK FROM date_close) - 1 WEEK
)