From 2bd20e3115ee232d040061d6e8c6aec0279c6653 Mon Sep 17 00:00:00 2001 From: Doug Beatty <44704949+dbeatty10@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:39:24 -0600 Subject: [PATCH 1/2] Cross-database `cast` macro --- .../cross-database-macros.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/website/docs/reference/dbt-jinja-functions/cross-database-macros.md b/website/docs/reference/dbt-jinja-functions/cross-database-macros.md index 334bcfe5760..8bf692dcf92 100644 --- a/website/docs/reference/dbt-jinja-functions/cross-database-macros.md +++ b/website/docs/reference/dbt-jinja-functions/cross-database-macros.md @@ -54,6 +54,7 @@ Please make sure to take a look at the [SQL expressions section](#sql-expression - [bool\_or](#bool_or) - [listagg](#listagg) - [Cast functions](#cast-functions) + - [cast](#cast) - [cast\_bool\_to\_text](#cast_bool_to_text) - [safe\_cast](#safe_cast) - [Date and time functions](#date-and-time-functions) @@ -101,6 +102,7 @@ Please make sure to take a look at the [SQL expressions section](#sql-expression - [bool\_or](#bool_or) - [listagg](#listagg) - [Cast functions](#cast-functions) + - [cast](#cast) - [cast\_bool\_to\_text](#cast_bool_to_text) - [safe\_cast](#safe_cast) - [Date and time functions](#date-and-time-functions) @@ -169,6 +171,7 @@ Please make sure to take a look at the [SQL expressions section](#sql-expression - [listagg](#listagg) [**Cast functions**](#cast-functions) +- [cast](#cast) - [cast_bool_to_text](#cast_bool_to_text) - [safe_cast](#safe_cast) @@ -777,6 +780,34 @@ array_to_string( ## Cast functions +### cast + +**Availability**: +dbt v1.8 or higher + +__Args__: + + * `field`: [attribute name or expression](#sql-expressions). + * `type`: data type to convert to + +This macro casts a value to the specified data type. Unlike [safe\_cast](#safe_cast), this macro will raise an error when the cast fails. + +**Usage**: + +```sql +{{ dbt.cast("column_1", api.Column.translate_type("string")) }} +{{ dbt.cast("column_2", api.Column.translate_type("integer")) }} +{{ dbt.cast("'2016-03-09'", api.Column.translate_type("date")) }} +``` + +**Sample Output (PostgreSQL)**: + +```sql + cast(column_1 as TEXT) + cast(column_2 as INT) + cast('2016-03-09' as date) +``` + ### cast_bool_to_text __Args__: From cfec9ec5cf789f36ed5db1a9bcfdc9c716353a6f Mon Sep 17 00:00:00 2001 From: Mirna Wong <89008547+mirnawong1@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:58:41 +0100 Subject: [PATCH 2/2] Update cross-database-macros.md addversions --- .../reference/dbt-jinja-functions/cross-database-macros.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/website/docs/reference/dbt-jinja-functions/cross-database-macros.md b/website/docs/reference/dbt-jinja-functions/cross-database-macros.md index 8bf692dcf92..e13dba0bdec 100644 --- a/website/docs/reference/dbt-jinja-functions/cross-database-macros.md +++ b/website/docs/reference/dbt-jinja-functions/cross-database-macros.md @@ -783,7 +783,9 @@ array_to_string( ### cast **Availability**: -dbt v1.8 or higher +dbt v1.8 or higher. For more information, select the version from the documentation navigation menu. + + __Args__: @@ -808,6 +810,8 @@ This macro casts a value to the specified data type. Unlike [safe\_cast](#safe_c cast('2016-03-09' as date) ``` + + ### cast_bool_to_text __Args__: