Skip to content

Commit

Permalink
Cross-database cast macro (#5278)
Browse files Browse the repository at this point in the history
[Preview](https://docs-getdbt-com-git-dbeatty10-patch-3-dbt-labs.vercel.app/reference/dbt-jinja-functions/cross-database-macros)

> [!NOTE]  
> I didn't make much of an attempt at versioning this thoughtfully. So
please update this as-needed to bring it in line with expectations.

## What are you changing in this pull request and why?

dbt-labs/dbt-adapters#84 was implemented in
commit
dbt-labs/dbt-adapters@5a50be7
within PR dbt-labs/dbt-adapters#55

So this docs PR adds it to the listing of [cross-database
macros](https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros).

## Checklist
- [x] Review the [Content style
guide](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/content-style-guide.md)
so my content adheres to these guidelines.
  • Loading branch information
mirnawong1 authored Apr 16, 2024
2 parents 92ae6d0 + cfec9ec commit bf87975
Showing 1 changed file with 35 additions and 0 deletions.
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

0 comments on commit bf87975

Please sign in to comment.