SlideShare ist ein Scribd-Unternehmen logo
1 von 7
SQL Speed Dating
How to get the DATE you want
1
Dunia Goh
https://au.linkedin.com/in/duniagoh
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
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
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
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
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
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

Weitere ähnliche Inhalte

Ähnlich wie SQL speed dating by Dunia Goh

IBM THINK 2019 - Self-Service Cloud Data Management with SQL
IBM THINK 2019 - Self-Service Cloud Data Management with SQL IBM THINK 2019 - Self-Service Cloud Data Management with SQL
IBM THINK 2019 - Self-Service Cloud Data Management with SQL Torsten Steinbach
 
How to document a database
How to document a databaseHow to document a database
How to document a databasePiotr Kononow
 
Ai based analytics in the cloud
Ai based analytics in the cloudAi based analytics in the cloud
Ai based analytics in the cloudSvetlin Stanchev
 
FlockData Overview
FlockData OverviewFlockData Overview
FlockData OverviewFlockData
 
SALES BASED DATA EXTRACTION FOR BUSINESS INTELLIGENCE
SALES BASED DATA EXTRACTION FOR BUSINESS INTELLIGENCESALES BASED DATA EXTRACTION FOR BUSINESS INTELLIGENCE
SALES BASED DATA EXTRACTION FOR BUSINESS INTELLIGENCEcscpconf
 
#SPSTC Maximizing the SharePoint User Experience with Free 3rd Party jQuery L...
#SPSTC Maximizing the SharePoint User Experience with Free 3rd Party jQuery L...#SPSTC Maximizing the SharePoint User Experience with Free 3rd Party jQuery L...
#SPSTC Maximizing the SharePoint User Experience with Free 3rd Party jQuery L...Mark Rackley
 
Key projects Data Science and Engineering
Key projects Data Science and EngineeringKey projects Data Science and Engineering
Key projects Data Science and EngineeringVijayananda Mohire
 
Key projects Data Science and Engineering
Key projects Data Science and EngineeringKey projects Data Science and Engineering
Key projects Data Science and EngineeringVijayananda Mohire
 
StrategiesForUsingMetadata
StrategiesForUsingMetadataStrategiesForUsingMetadata
StrategiesForUsingMetadataSuite Solutions
 
IBM THINK 2019 - Cloud-Native Clickstream Analysis in IBM Cloud
IBM THINK 2019 - Cloud-Native Clickstream Analysis in IBM CloudIBM THINK 2019 - Cloud-Native Clickstream Analysis in IBM Cloud
IBM THINK 2019 - Cloud-Native Clickstream Analysis in IBM CloudTorsten Steinbach
 
Recharge_report_Automation
Recharge_report_AutomationRecharge_report_Automation
Recharge_report_AutomationKIIT
 
Working with CloudStack Usage Data - CCCEU13
Working with CloudStack Usage Data - CCCEU13Working with CloudStack Usage Data - CCCEU13
Working with CloudStack Usage Data - CCCEU13ShapeBlue
 
Working with CloudStack Usage Data
Working with CloudStack Usage DataWorking with CloudStack Usage Data
Working with CloudStack Usage DataTariq Iqbal
 
The Economics of SQL on Hadoop
The Economics of SQL on HadoopThe Economics of SQL on Hadoop
The Economics of SQL on HadoopDatameer
 
IBM THINK 2018 - IBM Cloud SQL Query Introduction
IBM THINK 2018 - IBM Cloud SQL Query IntroductionIBM THINK 2018 - IBM Cloud SQL Query Introduction
IBM THINK 2018 - IBM Cloud SQL Query IntroductionTorsten Steinbach
 
(OTW13) Agile Data Warehousing: Introduction to Data Vault Modeling
(OTW13) Agile Data Warehousing: Introduction to Data Vault Modeling(OTW13) Agile Data Warehousing: Introduction to Data Vault Modeling
(OTW13) Agile Data Warehousing: Introduction to Data Vault ModelingKent Graziano
 
What’s New in Imply 3.3 & Apache Druid 0.18
What’s New in Imply 3.3 & Apache Druid 0.18What’s New in Imply 3.3 & Apache Druid 0.18
What’s New in Imply 3.3 & Apache Druid 0.18Imply
 
Google.Test4prep.Professional-Data-Engineer.v2038q.pdf
Google.Test4prep.Professional-Data-Engineer.v2038q.pdfGoogle.Test4prep.Professional-Data-Engineer.v2038q.pdf
Google.Test4prep.Professional-Data-Engineer.v2038q.pdfssuser22b701
 

Ähnlich wie SQL speed dating by Dunia Goh (20)

IBM THINK 2019 - Self-Service Cloud Data Management with SQL
IBM THINK 2019 - Self-Service Cloud Data Management with SQL IBM THINK 2019 - Self-Service Cloud Data Management with SQL
IBM THINK 2019 - Self-Service Cloud Data Management with SQL
 
How to document a database
How to document a databaseHow to document a database
How to document a database
 
Ai based analytics in the cloud
Ai based analytics in the cloudAi based analytics in the cloud
Ai based analytics in the cloud
 
FlockData Overview
FlockData OverviewFlockData Overview
FlockData Overview
 
SALES BASED DATA EXTRACTION FOR BUSINESS INTELLIGENCE
SALES BASED DATA EXTRACTION FOR BUSINESS INTELLIGENCESALES BASED DATA EXTRACTION FOR BUSINESS INTELLIGENCE
SALES BASED DATA EXTRACTION FOR BUSINESS INTELLIGENCE
 
#SPSTC Maximizing the SharePoint User Experience with Free 3rd Party jQuery L...
#SPSTC Maximizing the SharePoint User Experience with Free 3rd Party jQuery L...#SPSTC Maximizing the SharePoint User Experience with Free 3rd Party jQuery L...
#SPSTC Maximizing the SharePoint User Experience with Free 3rd Party jQuery L...
 
Sql Portfolio
Sql PortfolioSql Portfolio
Sql Portfolio
 
Key projects Data Science and Engineering
Key projects Data Science and EngineeringKey projects Data Science and Engineering
Key projects Data Science and Engineering
 
Key projects Data Science and Engineering
Key projects Data Science and EngineeringKey projects Data Science and Engineering
Key projects Data Science and Engineering
 
StrategiesForUsingMetadata
StrategiesForUsingMetadataStrategiesForUsingMetadata
StrategiesForUsingMetadata
 
IBM THINK 2019 - Cloud-Native Clickstream Analysis in IBM Cloud
IBM THINK 2019 - Cloud-Native Clickstream Analysis in IBM CloudIBM THINK 2019 - Cloud-Native Clickstream Analysis in IBM Cloud
IBM THINK 2019 - Cloud-Native Clickstream Analysis in IBM Cloud
 
Salesforce connect
Salesforce connectSalesforce connect
Salesforce connect
 
Recharge_report_Automation
Recharge_report_AutomationRecharge_report_Automation
Recharge_report_Automation
 
Working with CloudStack Usage Data - CCCEU13
Working with CloudStack Usage Data - CCCEU13Working with CloudStack Usage Data - CCCEU13
Working with CloudStack Usage Data - CCCEU13
 
Working with CloudStack Usage Data
Working with CloudStack Usage DataWorking with CloudStack Usage Data
Working with CloudStack Usage Data
 
The Economics of SQL on Hadoop
The Economics of SQL on HadoopThe Economics of SQL on Hadoop
The Economics of SQL on Hadoop
 
IBM THINK 2018 - IBM Cloud SQL Query Introduction
IBM THINK 2018 - IBM Cloud SQL Query IntroductionIBM THINK 2018 - IBM Cloud SQL Query Introduction
IBM THINK 2018 - IBM Cloud SQL Query Introduction
 
(OTW13) Agile Data Warehousing: Introduction to Data Vault Modeling
(OTW13) Agile Data Warehousing: Introduction to Data Vault Modeling(OTW13) Agile Data Warehousing: Introduction to Data Vault Modeling
(OTW13) Agile Data Warehousing: Introduction to Data Vault Modeling
 
What’s New in Imply 3.3 & Apache Druid 0.18
What’s New in Imply 3.3 & Apache Druid 0.18What’s New in Imply 3.3 & Apache Druid 0.18
What’s New in Imply 3.3 & Apache Druid 0.18
 
Google.Test4prep.Professional-Data-Engineer.v2038q.pdf
Google.Test4prep.Professional-Data-Engineer.v2038q.pdfGoogle.Test4prep.Professional-Data-Engineer.v2038q.pdf
Google.Test4prep.Professional-Data-Engineer.v2038q.pdf
 

Kürzlich hochgeladen

怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制vexqp
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...gajnagarg
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格q6pzkpark
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRajesh Mondal
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schscnajjemba
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubaikojalkojal131
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...Elaine Werffeli
 
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATIONCapstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATIONLakpaYanziSherpa
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareGraham Ware
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...nirzagarg
 
Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1ranjankumarbehera14
 
Data Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdfData Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdftheeltifs
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Klinik kandungan
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...nirzagarg
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With OrangePredicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With OrangeThinkInnovation
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制vexqp
 

Kürzlich hochgeladen (20)

Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATIONCapstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
 
Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1
 
Data Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdfData Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdf
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With OrangePredicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
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