# Conversion functions

[Read this page in HTML](https://www.scopedb.io/docs/reference/functions/conversion)

Converts values to a specified data type.

## Syntax

```scopeql
CAST(<expr> AS <type>)
<expr>::<type>
TRY(CAST(<expr> AS <type>))
TRY(<expr>::<type>)
```

## Arguments

| Argument | Type      | Description                                                                                                       |
| -------- | --------- | ----------------------------------------------------------------------------------------------------------------- |
| `<expr>` | Any type  | Value to convert.                                                                                                 |
| `<type>` | Type name | Target [data type](https://www.scopedb.io/docs/reference/data-types.md), such as `int`, `string`, or `timestamp`. |

## Returns

Returns the target type. A NULL input remains NULL. A failed conversion raises an error unless wrapped in `TRY(...)`.

## Usage notes

### CAST, ::

`CAST` and `::` perform the same conversion. The `::` operator binds more tightly than arithmetic, so use `(price_cents * quantity)::string` to cast the full line amount. See [data type conversion](https://www.scopedb.io/docs/reference/data-types/conversion.md) for supported conversions.

### TRY CAST

`TRY(...)` returns NULL when evaluating the cast fails. It does not make invalid syntax, unknown columns, or invalid function signatures valid. Use `CATCH(<expr>)` to inspect an evaluation error as text; it returns NULL if the expression succeeds.

## Examples

### Convert producer measurements

Convert a text amount into integer cents, and interpret a nanosecond timestamp supplied by a producer.

```scopeql
SELECT
    CAST('7900' AS int) AS total_cents,
    1780308000000000000::timestamp AS event_time
```

```text
+-------------+----------------------+
| total_cents | event_time           |
+-------------+----------------------+
| 7900        | 2026-06-01T10:00:00Z |
+-------------+----------------------+
```

### Keep malformed measurements visible

A producer sends duration as text, but one event contains a non-numeric value. Convert valid durations while keeping the bad record visible.

```scopeql
VALUES ('evt-17', '245'), ('evt-42', 'unknown'), ('evt-63', '1800')
SELECT $0 AS event_id, $1 AS raw_duration
SELECT event_id, raw_duration, TRY(raw_duration::int) AS duration_ms
```

```text
+----------+--------------+-------------+
| event_id | raw_duration | duration_ms |
+----------+--------------+-------------+
| evt-17   | 245          | 245         |
| evt-42   | unknown      | NULL        |
| evt-63   | 1800         | 1800        |
+----------+--------------+-------------+
```

Check NULL conversion results before aggregation; otherwise an aggregate that skips NULL values could silently omit a malformed measurement.

### Diagnose conversion errors

Pair `TRY` and `CATCH` to return converted durations alongside any conversion errors.

```scopeql
VALUES ('evt-17', '245'), ('evt-42', 'unknown'), ('evt-64', NULL)
SELECT $0 AS event_id, $1 AS raw_duration
SELECT
    event_id,
    TRY(raw_duration::int) AS duration_ms,
    CATCH(raw_duration::int) AS conversion_error
ORDER BY event_id
```

```text
+----------+-------------+----------------------------------------------------------------+
| event_id | duration_ms | conversion_error                                               |
+----------+-------------+----------------------------------------------------------------+
| evt-17   | 245         | NULL                                                           |
| evt-42   | NULL        | cannot convert `unknown` to int: invalid digit found in string |
| evt-64   | NULL        | NULL                                                           |
+----------+-------------+----------------------------------------------------------------+
```

The invalid duration produces an error message. The missing duration produces NULL in both columns because casting NULL succeeds.

## Related contents

* [Data type conversion](https://www.scopedb.io/docs/reference/data-types/conversion.md)
