Skip to content

Commit

Permalink
Merge pull request #98 from lucaslortiz/main
Browse files Browse the repository at this point in the history
add dbt_utils/sqlserver__generate_surrogate_key macro to fix concat bug
  • Loading branch information
sdebruyn authored Sep 7, 2024
2 parents 18a822f + a647364 commit f2b1cbe
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions macros/dbt_utils/sql/generate_surrogate_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{%- macro sqlserver__generate_surrogate_key(field_list) -%}

{%- if var('surrogate_key_treat_nulls_as_empty_strings', False) -%}
{%- set default_null_value = "" -%}
{%- else -%}
{%- set default_null_value = '_dbt_utils_surrogate_key_null_' -%}
{%- endif -%}

{%- set fields = [] -%}

{%- for field in field_list -%}

{%- do fields.append(
"coalesce(cast(" ~ field ~ " as " ~ dbt.type_string() ~ "), '" ~ default_null_value ~"')"
) -%}

{%- if not loop.last %}
{%- do fields.append("'-'") -%}
{%- endif -%}

{%- endfor -%}

{%- if fields|length > 1 %}
{{ dbt.hash(dbt.concat(fields)) }}
{%- else -%}
{{ dbt.hash(fields[0]) }}
{%- endif -%}

{%- endmacro -%}

0 comments on commit f2b1cbe

Please sign in to comment.