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

# Conversion

# Conversion functions

Trino will implicitly convert numeric and character values to the
correct type if such a conversion is possible. Trino will not convert
between character and numeric types. For example, a query that expects
a varchar will not automatically convert a bigint value to an
equivalent varchar.

When necessary, values can be explicitly cast to a particular type.

## Conversion functions

### cast

```
cast(value AS type) -> type
```

Explicitly cast a value as a type. This can be used to cast a
varchar to a numeric value type and vice versa.

### try\_cast

```
try_cast(value AS type) -> type
```

Like [cast](#cast), but returns null if the cast fails.

## Formatting

### format

```
format(format, args...) -> varchar
```

Returns a formatted string using the specified [format string](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Formatter.html#syntax)
and arguments:

```sql theme={null}
SELECT format('%s%%', 123);
-- '123%'

SELECT format('%.5f', pi());
-- '3.14159'

SELECT format('%03d', 8);
-- '008'

SELECT format('%,.2f', 1234567.89);
-- '1,234,567.89'

SELECT format('%-7s,%7s', 'hello', 'world');
-- 'hello  ,  world'

SELECT format('%2$s %3$s %1$s', 'a', 'b', 'c');
-- 'b c a'

SELECT format('%1$tA, %1$tB %1$te, %1$tY', date '2006-07-04');
-- 'Tuesday, July 4, 2006'
```

### format\_number

```
format_number(number) -> varchar
```

Returns a formatted string using a unit symbol:

```sql theme={null}
SELECT format_number(123456); -- '123K'
SELECT format_number(1000000); -- '1M'
```

## Data size

The `parse_data_size` function supports the following units:

| Unit | Description | Value            |
| ---- | ----------- | ---------------- |
| `B`  | Bytes       | 1                |
| `kB` | Kilobytes   | 1024             |
| `MB` | Megabytes   | 1024<sup>2</sup> |
| `GB` | Gigabytes   | 1024<sup>3</sup> |
| `TB` | Terabytes   | 1024<sup>4</sup> |
| `PB` | Petabytes   | 1024<sup>5</sup> |
| `EB` | Exabytes    | 1024<sup>6</sup> |
| `ZB` | Zettabytes  | 1024<sup>7</sup> |
| `YB` | Yottabytes  | 1024<sup>8</sup> |

### parse\_data\_size

```
parse_data_size(string) -> decimal(38)
```

Parses `string` of format `value unit` into a number, where
`value` is the fractional number of `unit` values:

```sql theme={null}
SELECT parse_data_size('1B'); -- 1
SELECT parse_data_size('1kB'); -- 1024
SELECT parse_data_size('1MB'); -- 1048576
SELECT parse_data_size('2.3MB'); -- 2411724
```

## Miscellaneous

### typeof

```
typeof(expr) -> varchar
```

Returns the name of the type of the provided expression:

```sql theme={null}
SELECT typeof(123); -- integer
SELECT typeof('cat'); -- varchar(3)
SELECT typeof(cos(2) + 1.5); -- double
```
