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

Cross-database cast macro #5278

Merged
merged 3 commits into from
Apr 16, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -777,6 +780,38 @@ array_to_string(

## Cast functions

### cast

**Availability**:
dbt v1.8 or higher. For more information, select the version from the documentation navigation menu.

<VersionBlock firstVersion="1.8">

__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)
```

</VersionBlock>

### cast_bool_to_text
__Args__:

Expand Down
Loading