SlideShare a Scribd company logo
1 of 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

More Related Content

Similar to 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
Working with CloudStack Usage DataWorking with CloudStack Usage Data
Working with CloudStack Usage DataTariq Iqbal
 
Working with CloudStack Usage Data - CCCEU13
Working with CloudStack Usage Data - CCCEU13Working with CloudStack Usage Data - CCCEU13
Working with CloudStack Usage Data - CCCEU13ShapeBlue
 
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
 

Similar to 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
Working with CloudStack Usage DataWorking with CloudStack Usage Data
Working with CloudStack Usage Data
 
Working with CloudStack Usage Data - CCCEU13
Working with CloudStack Usage Data - CCCEU13Working with CloudStack Usage Data - CCCEU13
Working with CloudStack Usage Data - CCCEU13
 
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
 

Recently uploaded

Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...Klinik Aborsi
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证pwgnohujw
 
1:1原版定制伦敦政治经济学院毕业证(LSE毕业证)成绩单学位证书留信学历认证
1:1原版定制伦敦政治经济学院毕业证(LSE毕业证)成绩单学位证书留信学历认证1:1原版定制伦敦政治经济学院毕业证(LSE毕业证)成绩单学位证书留信学历认证
1:1原版定制伦敦政治经济学院毕业证(LSE毕业证)成绩单学位证书留信学历认证dq9vz1isj
 
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeBoston Institute of Analytics
 
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...mikehavy0
 
一比一原版西悉尼大学毕业证成绩单如何办理
一比一原版西悉尼大学毕业证成绩单如何办理一比一原版西悉尼大学毕业证成绩单如何办理
一比一原版西悉尼大学毕业证成绩单如何办理pyhepag
 
edited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfedited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfgreat91
 
How to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsHow to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsBrainSell Technologies
 
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...Valters Lauzums
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshareraiaryan448
 
一比一原版(Monash毕业证书)莫纳什大学毕业证成绩单如何办理
一比一原版(Monash毕业证书)莫纳什大学毕业证成绩单如何办理一比一原版(Monash毕业证书)莫纳什大学毕业证成绩单如何办理
一比一原版(Monash毕业证书)莫纳什大学毕业证成绩单如何办理pyhepag
 
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...ssuserf63bd7
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchersdarmandersingh4580
 
Digital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae CoolbethDigital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae CoolbethSamantha Rae Coolbeth
 
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...Amil baba
 
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证ppy8zfkfm
 
Sensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor NetworksSensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor NetworksBoston Institute of Analytics
 
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一fztigerwe
 
The Significance of Transliteration Enhancing
The Significance of Transliteration EnhancingThe Significance of Transliteration Enhancing
The Significance of Transliteration Enhancingmohamed Elzalabany
 

Recently uploaded (20)

Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
 
1:1原版定制伦敦政治经济学院毕业证(LSE毕业证)成绩单学位证书留信学历认证
1:1原版定制伦敦政治经济学院毕业证(LSE毕业证)成绩单学位证书留信学历认证1:1原版定制伦敦政治经济学院毕业证(LSE毕业证)成绩单学位证书留信学历认证
1:1原版定制伦敦政治经济学院毕业证(LSE毕业证)成绩单学位证书留信学历认证
 
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
 
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
Abortion Clinic in Randfontein +27791653574 Randfontein WhatsApp Abortion Cli...
 
一比一原版西悉尼大学毕业证成绩单如何办理
一比一原版西悉尼大学毕业证成绩单如何办理一比一原版西悉尼大学毕业证成绩单如何办理
一比一原版西悉尼大学毕业证成绩单如何办理
 
edited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfedited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdf
 
How to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsHow to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data Analytics
 
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshare
 
一比一原版(Monash毕业证书)莫纳什大学毕业证成绩单如何办理
一比一原版(Monash毕业证书)莫纳什大学毕业证成绩单如何办理一比一原版(Monash毕业证书)莫纳什大学毕业证成绩单如何办理
一比一原版(Monash毕业证书)莫纳什大学毕业证成绩单如何办理
 
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchers
 
Digital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae CoolbethDigital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
 
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
 
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
 
Sensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor NetworksSensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
 
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
 
123.docx. .
123.docx.                                 .123.docx.                                 .
123.docx. .
 
The Significance of Transliteration Enhancing
The Significance of Transliteration EnhancingThe Significance of Transliteration Enhancing
The Significance of Transliteration Enhancing
 

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