Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement charindex macro DNA-15944 [DNA-20680] #78

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This dbt package contains macros for SQL functions to run the dbt project on mul

- [Multiple databases](#Multiple-databases)
- [as_varchar](#as_varchar-source)
- [charindex](#charindex-source)
- [create_index](#create_index-source)
- [date_from_timestamp](#date_from_timestamp-source)
- [datediff](#datediff-source)
Expand Down Expand Up @@ -61,6 +62,12 @@ This macro converts a string to the data type `nvarchar(2000)` for SQL Server. U
Usage:
`{{ pm_utils.as_varchar('[expression]') }}`

#### charindex ([source](macros/multiple_databases/charindex.sql))
This macro returns the starting position of the first occurrence of a string in another string. The search is not case-sensitive. If the string is not found, the function returns 0. This macro can be used to check whether a string contains another string.

Usage:
`{{ pm_utils.charindex('[expression_to_find]', '[field]', '[start_location]') }}`

#### create_index ([source](macros/multiple_databases/create_index.sql))
This macro creates a clustered columnstore index on the current model for SQL Server. This macro should be used in a dbt post-hook.

Expand Down
21 changes: 21 additions & 0 deletions macros/multiple_databases/charindex.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{%- macro charindex(expression_to_find, field, start_location=None) -%}
case
when len('{{ expression_to_find }}') > 0
then
{% if start_location is none -%}
{% if target.type == 'databricks' -%}
position('{{ expression_to_find }}', {{ field }})
{% else -%}
charindex('{{ expression_to_find }}', {{ field }})
{% endif -%}
{% else -%}
{% if target.type == 'databricks' -%}
position('{{ expression_to_find }}', {{ field }}, {{ start_location }})
{% else -%}
charindex('{{ expression_to_find }}', {{ field }}, {{ start_location }})
{% endif -%}
{% endif -%}
else
0
end
{%- endmacro -%}