diff --git a/content/en/logs/workspaces/sql_reference.md b/content/en/logs/workspaces/sql_reference.md index 62c9c23f5ff60..6bec77d2b0005 100644 --- a/content/en/logs/workspaces/sql_reference.md +++ b/content/en/logs/workspaces/sql_reference.md @@ -90,13 +90,13 @@ The following SQL functions are supported. For Window function, see the separate | `cast(value AS type)` | type | Converts the given value to the specified data type. | | `length(string s)` | integer | Returns the number of characters in the string. | | `trim(string s)` | string | Removes leading and trailing whitespace from the string. | -| `replace(string s, from_string s1, to_string s2)`| string | Replaces occurrences of a substring within a string with another substring. | -| `substring(string s, start_position_int i, length_int l)` | string | Extracts a substring from a string, starting at a given position and for a specified length. | -| `extract(field from timestamp/interval)` | numeric | Extracts a part of a date or time field (such as year or month) from a timestamp or interval. | -| `to_timestamp(numeric n)` | timestamp with time zone | Converts a numeric value to a timestamp with time zone. | -| `to_char(timestamp t / interval i / numeric n, format f)` | string | Converts a timestamp, interval, or numeric value to a string using a format.| -| `date_trunc(field f, source [, time_zone])` | timestamp [with time zone] / interval | Truncates a timestamp or interval to a specified precision. | -| `regexp_like(string s, pattern p [flags])` | boolean | Evaluates if a string matches a regular expression pattern. | +| `replace(string s, string from, string to)` | string | Replaces occurrences of a substring within a string with another substring. | +| `substring(string s, int start, int length)` | string | Extracts a substring from a string, starting at a given position and for a specified length. | +| `extract(unit from timestamp/interval)` | numeric | Extracts a part of a date or time field (such as year or month) from a timestamp or interval. | +| `to_timestamp(string timestamp, string format)` | timestamp | Converts a string to a timestamp according to the given format. | +| `to_char(timestamp t, string format)` | string | Converts a timestamp to a string according to the given format. | +| `date_trunc(string unit, timestamp t)` | timestamp | Truncates a timestamp to a specified precision based on the provided unit. | +| `regexp_like(string s, pattern p)` | boolean | Evaluates whether a string matches a regular expression pattern. | {{% collapse-content title="Examples" level="h3" %}} @@ -177,6 +177,13 @@ FROM users {{< /code-block >}} ### `CAST` + +Supported cast target types: +- `BIGINT` +- `DECIMAL` +- `TIMESTAMP` +- `VARCHAR` + {{< code-block lang="sql" >}} SELECT CAST(order_id AS VARCHAR) AS order_id_string, @@ -225,6 +232,23 @@ FROM {{< /code-block >}} ### `EXTRACT` + +Supported extraction units: +| Literal | Input Type | Description | +| ------------------| ------------------------ | -------------------------------------------- | +| `day` | `timestamp` / `interval` | day of the month | +| `dow` | `timestamp` | day of the week `1` (Monday) to `7` (Sunday) | +| `doy` | `timestamp` | day of the year (`1` - `366`) | +| `hour` | `timestamp` / `interval` | hour of the day (`0` - `23`) | +| `minute` | `timestamp` / `interval` | minute of the hour (`0` - `59`) | +| `second` | `timestamp` / `interval` | second of the minute (`0` - `59`) | +| `week` | `timestamp` | week of the year (`1` - `53`) | +| `month` | `timestamp` | month of the year (`1` - `12`) | +| `quarter` | `timestamp` | quarter of the year (`1` - `4`) | +| `year` | `timestamp` | year | +| `timezone_hour` | `timestamp` | hour of the time zone offset | +| `timezone_minute` | `timestamp` | minute of the time zone offset | + {{< code-block lang="sql" >}} SELECT extract(year FROM purchase_date) AS purchase_year @@ -233,14 +257,50 @@ FROM {{< /code-block >}} ### `TO_TIMESTAMP` + +Supported patterns for date/time formatting: +| Pattern | Description | +| ----------- | ------------------------------------ | +| `YYYY` | year (4 digits) | +| `YY` | year (2 digits) | +| `MM` | month number (01 - 12) | +| `DD` | day of month (01 - 31) | +| `HH24` | hour of day (00 - 23) | +| `HH12` | hour of day (01 - 12) | +| `HH` | hour of day (01 - 12) | +| `MI` | minute (00 - 59) | +| `SS` | second (00 - 59) | +| `MS` | millisecond (000 - 999) | +| `TZ` | time-zone abbreviation | +| `OF` | time-zone offset from UTC | +| `AM` / `am` | meridiem indicator (without periods) | +| `PM` / `pm` | meridiem indicator (without periods) | + {{< code-block lang="sql" >}} SELECT - to_timestamp(epoch_time) AS formatted_time -FROM - event_logs + to_timestamp('25/12/2025 04:23 pm', 'DD/MM/YYYY HH:MI am') AS ts {{< /code-block >}} ### `TO_CHAR` + +Supported patterns for date/time formatting: +| Pattern | Description | +| ----------- | ------------------------------------ | +| `YYYY` | year (4 digits) | +| `YY` | year (2 digits) | +| `MM` | month number (01 - 12) | +| `DD` | day of month (01 - 31) | +| `HH24` | hour of day (00 - 23) | +| `HH12` | hour of day (01 - 12) | +| `HH` | hour of day (01 - 12) | +| `MI` | minute (00 - 59) | +| `SS` | second (00 - 59) | +| `MS` | millisecond (000 - 999) | +| `TZ` | time-zone abbreviation | +| `OF` | time-zone offset from UTC | +| `AM` / `am` | meridiem indicator (without periods) | +| `PM` / `pm` | meridiem indicator (without periods) | + {{< code-block lang="sql" >}} SELECT to_char(order_date, 'MM-DD-YYYY') AS formatted_date @@ -249,6 +309,18 @@ FROM {{< /code-block >}} ### `DATE_TRUNC` + +Supported truncations: +- `milliseconds` +- `seconds` / `second` +- `minutes` / `minute` +- `hours` / `hour` +- `days` / `day` +- `weeks` / `week ` +- `months` / `month` +- `quarters` / `quarter` +- `years` / `year` + {{< code-block lang="sql" >}} SELECT date_trunc('month', event_time) AS month_start