Files
school_compare/pipeline/transform/macros/safe_numeric.sql
T

10 lines
391 B
SQL
Raw Normal View History

{#
safe_numeric(col)
Casts a string column to numeric, treating any non-numeric value as NULL.
Handles all EES suppression codes (z, c, x, q, u, etc.) without needing
an explicit list any string that doesn't look like a number becomes NULL.
#}
{% macro safe_numeric(col) -%}
CASE WHEN {{ col }} ~ '^-?[0-9]+(\.[0-9]+)?$' THEN {{ col }}::numeric ELSE NULL END
{%- endmacro %}