How can I change dates datatype from String to Date

Viewed 70

I want to change Dates datatype from String to Date, In Google BigQuery. I wrote this code:

SELECT PARSE_DATE("%x", Order_Date),PARSE_DATE("%x", Ship_Date) 
FROM `SALES_CHART`

I got this error:

Failed to parse input string "08-02-2010"

2 Answers

Use Supported format elements for DATE:

SELECT PARSE_DATE("%d-%m-%Y", "08-02-2010")

enter image description here

%A  The full weekday name.
%a  The abbreviated weekday name.
%B  The full month name.
%b or %h    The abbreviated month name.
%C  The century (a year divided by 100 and truncated to an integer) as a decimal number (00-99).
%D  The date in the format %m/%d/%y.
%d  The day of the month as a decimal number (01-31).
%e  The day of month as a decimal number (1-31); single digits are preceded by a space.
%F  The date in the format %Y-%m-%d.
%G  The ISO 8601 year with century as a decimal number. Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %G and %Y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge.
%g  The ISO 8601 year without century as a decimal number (00-99). Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %g and %y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge.
%j  The day of the year as a decimal number (001-366).
%m  The month as a decimal number (01-12).
%n  A newline character.
%Q  The quarter as a decimal number (1-4).
%t  A tab character.
%U  The week number of the year (Sunday as the first day of the week) as a decimal number (00-53).
%u  The weekday (Monday as the first day of the week) as a decimal number (1-7).
%V  The ISO 8601 week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is week 53 of the previous year, and the next week is week 1.
%W  The week number of the year (Monday as the first day of the week) as a decimal number (00-53).
%w  The weekday (Sunday as the first day of the week) as a decimal number (0-6).
%x  The date representation in MM/DD/YY format.
%Y  The year with century as a decimal number.
%y  The year without century as a decimal number (00-99), with an optional leading zero. Can be mixed with %C. If %C is not specified, years 00-68 are 2000s, while years 69-99 are 1900s.
%E4Y    Four-character years (0001 ... 9999). Note that %Y produces as many characters as it takes to fully render the year.

You can try following different option also:-

select cast ('2021-02-22' as date)
select cast ('2021-02-22 06:40:00' as datetime)
select cast ('2021-02-22 06:40:00' as timestamp)

Difference between Datetime & Timestamp you can refer it here.

Related