2022-10-28 17:43:07 +00:00
|
|
|
---
|
|
|
|
slug: /en/sql-reference/table-functions/format
|
|
|
|
sidebar_position: 56
|
|
|
|
sidebar_label: format
|
|
|
|
---
|
|
|
|
|
|
|
|
# format
|
|
|
|
|
2023-02-22 16:51:29 +00:00
|
|
|
Parses data from arguments according to specified input format. If structure argument is not specified, it's extracted from the data.
|
2022-10-28 17:43:07 +00:00
|
|
|
|
|
|
|
**Syntax**
|
|
|
|
|
|
|
|
``` sql
|
2023-02-22 16:51:29 +00:00
|
|
|
format(format_name, [structure], data)
|
2022-10-28 17:43:07 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters**
|
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- `format_name` — The [format](../../interfaces/formats.md#formats) of the data.
|
|
|
|
- `structure` - Structure of the table. Optional. Format 'column1_name column1_type, column2_name column2_type, ...'.
|
|
|
|
- `data` — String literal or constant expression that returns a string containing data in specified format
|
2022-10-28 17:43:07 +00:00
|
|
|
|
|
|
|
**Returned value**
|
|
|
|
|
2023-02-22 16:51:29 +00:00
|
|
|
A table with data parsed from `data` argument according to specified format and specified or extracted structure.
|
2022-10-28 17:43:07 +00:00
|
|
|
|
|
|
|
**Examples**
|
|
|
|
|
2023-02-22 16:51:29 +00:00
|
|
|
Without `structure` argument:
|
|
|
|
|
2022-10-28 17:43:07 +00:00
|
|
|
**Query:**
|
|
|
|
``` sql
|
2023-01-11 13:43:51 +00:00
|
|
|
SELECT * FROM format(JSONEachRow,
|
2022-10-28 17:43:07 +00:00
|
|
|
$$
|
|
|
|
{"a": "Hello", "b": 111}
|
|
|
|
{"a": "World", "b": 123}
|
|
|
|
{"a": "Hello", "b": 112}
|
|
|
|
{"a": "World", "b": 124}
|
|
|
|
$$)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Result:**
|
|
|
|
|
2023-01-11 15:08:11 +00:00
|
|
|
```response
|
2022-10-28 17:43:07 +00:00
|
|
|
┌───b─┬─a─────┐
|
|
|
|
│ 111 │ Hello │
|
|
|
|
│ 123 │ World │
|
|
|
|
│ 112 │ Hello │
|
|
|
|
│ 124 │ World │
|
|
|
|
└─────┴───────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
**Query:**
|
|
|
|
```sql
|
2023-01-11 13:43:51 +00:00
|
|
|
DESC format(JSONEachRow,
|
2022-10-28 17:43:07 +00:00
|
|
|
$$
|
|
|
|
{"a": "Hello", "b": 111}
|
|
|
|
{"a": "World", "b": 123}
|
|
|
|
{"a": "Hello", "b": 112}
|
|
|
|
{"a": "World", "b": 124}
|
|
|
|
$$)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Result:**
|
|
|
|
|
2023-01-11 15:08:11 +00:00
|
|
|
```response
|
2022-10-28 17:43:07 +00:00
|
|
|
┌─name─┬─type──────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
|
|
|
|
│ b │ Nullable(Float64) │ │ │ │ │ │
|
|
|
|
│ a │ Nullable(String) │ │ │ │ │ │
|
|
|
|
└──────┴───────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
|
|
|
|
```
|
|
|
|
|
2023-02-22 16:51:29 +00:00
|
|
|
With `structure` argument:
|
|
|
|
|
|
|
|
**Query:**
|
|
|
|
```sql
|
|
|
|
SELECT * FROM format(JSONEachRow, 'a String, b UInt32',
|
|
|
|
$$
|
|
|
|
{"a": "Hello", "b": 111}
|
|
|
|
{"a": "World", "b": 123}
|
|
|
|
{"a": "Hello", "b": 112}
|
|
|
|
{"a": "World", "b": 124}
|
|
|
|
$$)
|
|
|
|
```
|
|
|
|
|
|
|
|
**Result:**
|
|
|
|
```response
|
|
|
|
┌─a─────┬───b─┐
|
|
|
|
│ Hello │ 111 │
|
|
|
|
│ World │ 123 │
|
|
|
|
│ Hello │ 112 │
|
|
|
|
│ World │ 124 │
|
|
|
|
└───────┴─────┘
|
|
|
|
```
|
|
|
|
|
2022-10-28 17:43:07 +00:00
|
|
|
**See Also**
|
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- [Formats](../../interfaces/formats.md)
|