-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed date macro and usage in 2 models (#76)
* fixed date macro and usage in 2 models * comment
- Loading branch information
Showing
5 changed files
with
60 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,36 @@ | ||
{% macro validate_date(field_name) %} | ||
|
||
-- Attempt to validate the field as a date, return the original format | ||
-- We have data coming from several google sheets and from commcare. below macro makes sure every data column, no matter | ||
-- which table it is in or which source it is coming from is in the same appropriate format. | ||
-- Sometimes in google sheets, in one row someone enters 2024/10/12 and elsewhere someone enters 10-5-2024 it will standardize it | ||
case | ||
when {{ field_name }}::text ~ '^\s*\d{4}-\d{2}-\d{2}\s*$' then | ||
-- It strictly looks like a date (YYYY-MM-DD), safely cast to date | ||
-- Ensure NULL and empty strings are converted to NULL safely before anything else | ||
when {{ field_name }} is null or COALESCE({{ field_name }}::text, '') = '' then null::date | ||
|
||
-- If it's already a DATE, return it as is | ||
when pg_typeof({{ field_name }})::text = 'date' then | ||
{{ field_name }}::date | ||
|
||
-- Ensures that TIMESTAMP and TIMESTAMP WITH TIME ZONE are converted properly | ||
when pg_typeof({{ field_name }})::text like 'timestamp%' then | ||
{{ field_name }}::date | ||
else null | ||
end | ||
|
||
-- Handle standard YYYY-MM-DD format | ||
when COALESCE({{ field_name }}::text, '') ~ '^\d{4}-\d{2}-\d{2}$' then | ||
to_date({{ field_name }}::text, 'YYYY-MM-DD') | ||
|
||
-- Handle DD-MM-YYYY format | ||
when COALESCE({{ field_name }}::text, '') ~ '^\d{2}-\d{2}-\d{4}$' then | ||
to_date({{ field_name }}::text, 'DD-MM-YYYY') | ||
|
||
-- Handle MM/DD/YYYY format | ||
when COALESCE({{ field_name }}::text, '') ~ '^\d{2}/\d{2}/\d{4}$' then | ||
to_date({{ field_name }}::text, 'MM/DD/YYYY') | ||
|
||
-- Handle DD Mon YYYY format (e.g., 12 Jan 2024) | ||
when COALESCE({{ field_name }}::text, '') ~ '^\d{1,2} \w{3} \d{4}$' then | ||
to_date({{ field_name }}::text, 'DD Mon YYYY') | ||
|
||
-- If none of the formats match, return NULL | ||
else null::date | ||
end | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters