Parsing Date time in BigQuery - google-bigquery

Below is the date format for the 'date' field, which I'm getting from API to Bigquery as a string.
2020-09-17T00:00:00+03:00
I want to parse the date format to '%Y%m%d' and then extract it as ISOWEEK.
This is the code I came up with;
EXTRACT(ISOWEEK FROM PARSE_DATETIME("%Y%m%d", REGEXP_EXTRACT(date, r"...................")))
However, this gives me an error.
Could you guide me what needs change?
Thank you

Let's break down the steps here. The date input is a string of a date in a certain timezone (+03:00), for example "2020-09-17T00:00:00+03:00". Then on this string you can apply:
Keep the date on the correct timezone
DATE("2020-09-17T00:00:00+03:00", "+03:00")
Format the date with the desired format in a string
FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00"))
Parse the date from the previously formatted string
PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00")))
Finally extract the ISOWEEK from the date
EXTRACT(ISOWEEK FROM PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00"))))
So, this will give you the result you want.
select EXTRACT(ISOWEEK FROM PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE(date))))
You can also skip the "%Y/%m/%d" if it's not really helping you with something else and simply do
select EXTRACT(ISOWEEK FROM DATE("2020-09-17T00:00:00+03:00", "+03:00"))
which results to the same thing

Related

how to change date format to just year and month but must stay as a date value not varchar, or char..?

I read other similar question where the answer is to use FORMAT(getdate(),'yyy-MM') or something similar. However the problem for me in using anything like this, is that it changes the date type to a varchar or char. I need it to stay as datetype but just want Year and Month.. I tried the following..-> FORMAT(a.completeddate,'yyy-MM') which works to change to year and month but the date is no longer a datetype or date format. So when I try to do the following -> select #FirstCompletion = (A.completeddate) i get this error..Conversion failed when converting date and/or time from character string. Basically I need to convert the date column to year and month as date format so I can then pass values to variables using select #FirstCompletion = (A.completeddate) and set #secondMonth = DATEADD(month, 2, #FirstCompletion) which are Datetype variables.. Would appreciate any help I can get.. Thanks..

Convert string of YYYYMMDD to YYYY-MM-DD Date format in Snowflake

Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')

change the date format in bigquery

I have a date column as dd-mm-yyyy. I would like to convert it to yyyy/mm/dd in bigquery.I have written the following query:
SELECT cast(format(Date, 'yyyy/mm/dd') as string) as Date FROM t1.
The error is : Too many arguments to FORMAT for pattern "23/04/2020"; Expected 1; Got 2.
Can you please assist.
First you need to parse date from dd-mm-yyyy string and then format it as yyyy/mm/dd as in below
FORMAT_DATE('%Y/%m/%d', PARSE_DATE('%d-%m-%Y', day))
You can test, play with above using dummy data as in below example
#standardSQL
WITH `project.dataset.table` AS (
SELECT '15-01-2020' day UNION ALL
SELECT '05-10-2019'
)
SELECT day, FORMAT_DATE('%Y/%m/%d', PARSE_DATE('%d-%m-%Y', day)) AS formated_day
FROM `project.dataset.table`
with output
Row day formated_day
1 15-01-2020 2020/01/15
2 05-10-2019 2019/10/05
You want FORMAT_DATE, not FORMAT.
SELECT FORMAT_DATE("%Y/%m/%d", DATE "2008-12-25");
The reason you're having trouble with FORMAT is that you gave it a format string that doesn't take any parameters. Seeing this, the engine barfs-"I don't need any more parameters to render this string."

What is CHAR(7) in Teradata SQL in date format?

I am trying to get the idea of char(7) in the following SQL in Teradata:
SELECT current_date (FORMAT'YYYY-MM') (char(7));
as far as it forces the display of the correct format, exactly as specified.
When I try without char(7) at the end, the format is not as specified, e.g.:
SELECT current_date (FORMAT 'YYYY-MM');
returns the day as well, ignoring the format command:
As a subquestion - is this the correct way to display the month and the year from a date column? (with char(7))
I would suggest using TO_CHAR here:
SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM');
This takes the guesswork out of your problem, as the contract of TO_CHAR is to return a string representation of the input datetime using the format mask you specify. You do not need to worry about requesting a certain string width.

How to change date format in hive?

My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.