The document outlines 5 common scenarios for converting a date field stored in an undesirable format into a preferred date format of yyyy-mm-dd in SQL. The scenarios include: 1) dates stored as strings in the correct format, 2) dates stored as strings with invalid characters, 3) dates stored as incorrectly formatted strings, 4) dates stored as strings with extra time information, and 5) dates stored inconsistently across records. For each scenario, the document provides the SQL code to handle the conversion using functions like CAST, PARSE_DATE, CASE, and SUBSTR.
Abortion pills in Jeddah | +966572737505 | Get Cytotec
SQL speed dating by Dunia Goh
1. SQL Speed Dating
How to get the DATE you want
1
Dunia Goh
https://au.linkedin.com/in/duniagoh
2. Getting dates is an essential skill
for Reporting Analysts
We are often faced with data stored in an undesirable format
This article outlines 5 common scenarios and how to convert a field into the
preferred date format: yyyy-mm-dd
There are many ways to do things in SQL, but these are actual examples of how in
BigQuery I’ve converted a field to a DATE for use in another function, calculation or
just to correctly format for Tableau
SQL Speed Dating - by Dunia Goh
https://au.linkedin.com/in/duniagoh
2
3. Scenario 1
• Your Date_Field has been stored
as Type STRING and contains data
already in the correct format, E.g
2020-08-20
• Use: CAST
CAST(Date_Field AS DATE) as
Date_Field_1
You can also use CAST to get the DATE component from any field already in
types: DATETIME or TIMESTAMP. More information on CAST is available at:
https://cloud.google.com/bigquery/docs/reference/standard-
sql/conversion_rules
SQL Speed Dating - by Dunia Goh
https://au.linkedin.com/in/duniagoh
3
4. Scenario 2
• Your Date_Field has been stored
as Type STRING and contains data
already in the correct format, E.g
2020-08-20 but also invalid
characters such as a hash # symbol
when the date should be null
• Use: A combination of CASE and
CAST
CASE WHEN Date_Field = '#'
THEN NULL ELSE CAST(Date_Field
AS DATE) END as Date_Field_2
You can also find information for how to CASE at:
https://cloud.google.com/dataprep/docs/html/CASE-Function_90112182
SQL Speed Dating - by Dunia Goh
https://au.linkedin.com/in/duniagoh
4
5. Scenario 3
• Your Date_Field has been stored
as Type STRING and contains data
incorrectly formatted, E.g 2020-
08-20 has been stored as
20200820
• Use: PARSE_DATE
PARSE_DATE("%Y%m%d",
Date_Field AS Date_Field_3
You can also find other supported Format Elements for parsing dates at:
https://cloud.google.com/bigquery/docs/reference/standard-
sql/date_functions#parse_date
SQL Speed Dating - by Dunia Goh
https://au.linkedin.com/in/duniagoh
5
6. Scenario 4
• Your Date_Field has been stored
as Type STRING and contains data
incorrectly formatted, E.g 2020-
08-20 has been stored with time
on the end as 20200820082904
• Use: A combination of SUBSTR and
PARSE_DATE
PARSE_DATE("%Y%m%d",
SUBSTR(Date_Field, 1, 8) AS
Date_Field_4
You can also find information for how to use SUBSTR at:
https://cloud.google.com/dataprep/docs/html/SUBSTRING-
Function_57344730
SQL Speed Dating - by Dunia Goh
https://au.linkedin.com/in/duniagoh
6
7. Scenario 5
• Your Date_Field has been stored
as Type STRING and contains non-
standard data, E.g Some data is
showing as 2020-08-20, some
showing as 2020.08.18 and some
have notations afterwards like
2020.08.18 PH
• Use: A combination of CASE,
SUBSTR and PARSE_DATE
CASE WHEN SUBSTR(Date_Field,
5,1) = '.' THEN
PARSE_DATE('%Y.%m.%d',
SUBSTR(Date_Field,1,10))
ELSE PARSE_DATE('%Y-%m-
%d',SUBSTR(Date_Field,1,10))
END AS Date_Field_5
I hope you enjoyed your dates!
Copy the SQL code from each
scenario and replace the
Date_Field with your Field
SQL Speed Dating - by Dunia Goh
https://au.linkedin.com/in/duniagoh
7