> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hightouch.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Data types and casting

> Third-party APIs require the data you send to be of a specific data type. Unless otherwise specified, Hightouch takes the data from your source and leaves the data type unchanged to send to your destination. You can choose to cast data into different types using SQL.

## Why data types matter

When syncing data to a destination, their underlying APIs require the data you send to be correctly typed. For example, a `Name` field may require a `string` data type, while a `timestamp` field may require a particular `datetime` format. If you try to sync an incorrect type, for example, a `number` type for an identifier, when a `string` type is expected, APIs usually reject the request, and the sync fails.

When you define a [model](https://docs.hightouch.com/getting-started/concepts.md#models), Hightouch doesn't change the data types found in your [source](https://docs.hightouch.com/getting-started/concepts.md#sources) unless otherwise specified. To safeguard your syncs from failing, the data types your model returns must align with your destination's data type expectations. If your source data types don't match your destination's expectations, you can use [casting](#casting) while setting up your models.

## Data types

Hightouch recognizes the following data types for [model columns](#view-model-data-types):

* Boolean
* Number
* Big Integer (available for sources that support it)
* String
* Timestamp
* Date
* Object / Array

And the following for [destination fields](#view-destination-data-types):

* Boolean
* Number
* String
* Timestamp
* Date/time
* Associated field (linked to another table)
* Null value
* Unknown type

In the field mapper, Hightouch marks each column and destination field with an icon for its data type, so you can spot a type mismatch at a glance.

### View model data types

If you don't know the data types of the columns returned from your model, you can view them from the **Columns** tab on the model's overview page:

![A model's Columns tab showing each column's data type](https://mintcdn.com/hightouch-3c037897/L_vQfA0JQEpeGUUo/images/models/data-types-casting/model-columns.png?fit=max\&auto=format\&n=L_vQfA0JQEpeGUUo\&q=85\&s=cc9407fda3d555d76199b36f8f803d97)

You can also [change the data types](#casting-from-your-model-configuration) returned from a query here.

> [!WARNING]
>
> Make sure to read through the [primary key
> updates](https://docs.hightouch.com/models/creating-models.md#updating-a-primary-key) section before making
> any changes to your primary key.

> [!TIP]
>
> Hightouch intentionally stringifies your chosen [**primary
> key**](https://docs.hightouch.com/models/creating-models.md#unique-primary-key-requirement) column for
> enhanced performance during [change data
> capture](https://docs.hightouch.com/getting-started/concepts.md#change-data-capture-cdc). If you need to
> sync the primary key column as a non-string value, use [SQL
> aliasing](#sql-aliasing) in your model to create a new column specifically for
> syncing.

### View destination data types

When you set up [field mappings](https://docs.hightouch.com/syncs/mapping-data.md) in a sync, each column and destination field shows a data-type icon. Use these icons to confirm a source column's type matches the destination field's expected type before you sync.

![Data-type icons next to columns in the field mapper](https://mintcdn.com/hightouch-3c037897/L_vQfA0JQEpeGUUo/images/models/data-types-casting/field-type-icons.png?fit=max\&auto=format\&n=L_vQfA0JQEpeGUUo\&q=85\&s=7363e260e49e7a38dfa6bba2e60d8eec)

## Casting

For your data to reach its destination without error, its data type must match the data type required by the destination. You can use **[type casting](https://en.wikipedia.org/wiki/Type_conversion)** to change column data types. You have these options:

* Click and point casting from a model's **Columns** tab: suitable for all [modeling methods](https://docs.hightouch.com/models/creating-models.md)
* Casting via SQL in your model definition: suitable if you've built your model using the [SQL editor](https://docs.hightouch.com/models/sql-editor.md) and prefer to encode casting in your SQL query
* SQL aliasing: recommended if your [primary key](https://docs.hightouch.com/models/creating-models.md#unique-primary-key-requirement) data type is incompatible with the expected destination data type

### Casting from your model configuration

1. From the [**Models** overview page](https://app.hightouch.com/models?from=docs), select the model whose columns you want to cast.
2. Select the **Columns** tab.
3. Use the picklist to the right of each column to choose the data type you want for that column.

![The column type picklist open, showing the available data types](https://mintcdn.com/hightouch-3c037897/L_vQfA0JQEpeGUUo/images/models/data-types-casting/column-type-dropdown.png?fit=max\&auto=format\&n=L_vQfA0JQEpeGUUo\&q=85\&s=06687f0c05f91dc3908deb0b82bc15aa)

### Casting in SQL

You can cast values from one data type to another when writing the SQL query that defines a model.

For example, this query casts a column called `total_storage_used_mb` as an integer:

```sql theme={null}
SELECT *,
  CAST (total_storage_used_mb AS int)
FROM public.organizations;
```

For more on SQL casting, see [this guide on SQL type casting](https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver16/).

### SQL aliasing

If your model's primary key isn't a string, Hightouch stringifies it for enhanced record-matching performance. If stringification makes your primary key incompatible with the data type your destination is expecting, you can use **SQL aliasing** to create an additional field for mapping.

For example, if you use a field called `id` as your primary key, Hightouch stringifies its value to perform the matching and [diffing operations](https://docs.hightouch.com/getting-started/concepts.md#change-data-capture-cdc).

Many destinations expect a field called `id` to be a string. Some destinations, however, expect `id` to be an integer. This mismatch causes a data type conflict if you need to map your primary key in addition to using it for matching.

Using SQL aliasing and casting can solve this issue. This example query selects the `customer_id` column twice. The second time, it casts the column as an `int`, and aliases it as a new `id_for_mapping` column.

```sql theme={null}
SELECT customer_id, CAST(customer_id AS int) AS id_for_mapping
FROM customers;
```

The query results will now include a string `customer_id` column and an integer `id_for_mapping` column. You can then use `customer_id` as the primary key in your model setup and `id_for_mapping` for field mapping in a sync configuration.
