We are exporting data from our ERP system into a CSV file, but we noticed that the date format has changed to (1/31/2022, 1/1/2022, 31/7/2023)
and is stored in the table as a STRING type after processing.
How can we convert these dates into a consistent format, specifically YYYY-MM-DD in dbt model?
Database: MySQL
Note :
I have different approach but not working.
DATE_FORMAT(tdate, ‘%m/%d/%Y’) ,
DATE_FORMAT(tdate,‘%d/%m/%Y’)
thanks for reply.
i tried same in my dbt model getting error.
written mode like
{{ config(materialized=‘table’) }}
with date_convert as(
select
a.tdate,
-- coalesce(
-- DATE_FORMAT(STR_TO_DATE(a.tdate, '%m/%d/%Y'), '%Y-%m-%d'),
-- DATE_FORMAT(STR_TO_DATE(a.tdate, '%d/%m/%Y'), '%Y-%m-%d'),
-- A.TDATE) new_date,
case when STR_TO_DATE(a.tdate, '%m/%d/%Y') is not null
then DATE_FORMAT(STR_TO_DATE(a.tdate, '%m/%d/%Y'), '%Y-%m-%d')
when STR_TO_DATE(a.tdate, '%D/%m/%Y') is not null
then DATE_FORMAT(STR_TO_DATE(a.tdate, '%D/%m/%Y'), '%Y-%m-%d')
else null end AS NEW_DATE1
from {{ ref("date_check") }} a
)
select * from date_convert
error
(HY000): Incorrect datetime value: ‘31/7/2023’ for function str_to_date