Date format in BigQuery - google-bigquery

Edited: Want to convert the format.
I am kinda new in BigQuery, recently I was working on a project. I want to convert above type of format into yyyy/mm/dd format. How will I do that?

You can combine PARSE_DATE with FORMAT_DATE to get the desired output:
SELECT FORMAT_DATE("%Y/%m/%d",PARSE_DATE("%B %d, %Y","June 10, 2014")) AS date_str
PARSE_DATE will parse the provided string into a DATE type value in BQ format (YYYY-MM-DD) and then FORMAT_DATE will format this DATE to a string with the desired output.

In case someone is wondering how to convert the whole column;
FORMAT_DATE("%Y/%m/%d",PARSE_DATE("%B %d, %Y",ColumnName)) as DesiredColumnName

Related

How do I cast a timestamp column as a MM/DD/YYYY in BigQuery

I'm trying to cast the results of an entire column in BigQuery in MM/DD/YYYY format
Time_Column
2022-05-26T17:32:41.000Z
2022-05-28T06:34:23.000Z
Results:
We can try to use FORMAT_DATE function to make it, we can refer to this link Supported Format Elements For DATE to use your expected date format from the datetime type.
SELECT FORMAT_DATE("%m/%d/%Y", Time_Column)
FROM T

SQL Date Formatting from String

I am stuck trying to convert the following strings formatted like this 26-09-2021-02-54-03 (DD-MM-YYYY-hh-mm-ss) into timestamp or in this format YYYY-MM-DD HH:DD:SS in BigQuery. Any idea how to process?
I cannot use PARSE_TIMESTAMP() since there is no T in the string.
Thank you
You should be able to parse it with the following:
select '26-09-2021-02-54-03'
, PARSE_TIMESTAMP("%d-%m-%Y-%H-%M-%S", '26-09-2021-02-54-03')
, FORMAT_TIMESTAMP("%F %X",PARSE_TIMESTAMP("%d-%m-%Y-%H-%M-%S", '26-09-2021-02-54-03'))
For more information on the format elements see:
https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#supported_format_elements_for_timestamp

Bigquery cannot parse date from datetime

Bigquery cannot parse datetime from a date in spite of providing right format. Dates in my column are of the same format I used in the string:
SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S%z',"2021-03-22T14:00:00-03:00"))
This gives this error:
Failed to parse input string "2021-03-22T14:00:00-03:00"
I am trying to make this work by this answer
You should use %Ez instead of %z as in below
SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S%Ez',"2021-03-22T14:00:00-03:00"))
See Supported format elements for TIMESTAMP for more details

Getting null while converting string to date in spark sql

I have dates in the format '6/30/2020'. It is a string and I want to convert it into date format.
List of methods I have tried
Cast('6/30/2020' as date) #returns null
to_date('6/30/2020','yyyy/MM/dd') #returns null
I also tried splitting the string and then concatenating it into data.
After trying all this and putting all the possible combinations in the to_date function, I am still getting the answer as null.
Now I am confused as I have used all the functions to convert string to date.
Thanks in advance for your help.
The date format you used was incorrect. Try this:
select to_date('6/30/2020', 'M/dd/yyyy')
If you want to format your result, you can use date_format:
select date_format(to_date('6/30/2020', 'M/dd/yyyy'), 'yyyy/MM/dd')
Note that to_date converts a given string from the given format, while date_format converts a given date to the given format.

Cast String into Date in BigQuery when Date is in the following format: DD-mon-YYYY

I have a string that is a date and it is in this format 'dd-mon-yy' ie:
27-Jan-18
28-Jan-18
How do I convert it into date with this format 'dd/mm/yyyy' using Standard SQL in BigQuery?
If you want to convert dates from the string format to a particular date format, you will first have to convert the string to a date using the PARSE_DATE function in BigQuery and then use the FORMAT_DATE function to convert the date into the desired format. Here is a snippet of code that converts the string of type "DD-Mon-YY" to a date value of the format dd/mm/yyyy:
SELECT FORMAT_DATE("%d/%m/%E4Y", PARSE_DATE("%d-%b-%y", "name_of_column_with_string_dates")) as formatted_dates FROM `projectId.dataset.table`