Skip to content

Commit ab71efa

Browse files
committed
Update to-timestamp.md
1 parent e189b52 commit ab71efa

File tree

1 file changed

+42
-6
lines changed
  • docs/en/sql-reference/20-sql-functions/05-datetime-functions

1 file changed

+42
-6
lines changed

Diff for: docs/en/sql-reference/20-sql-functions/05-datetime-functions/to-timestamp.md

+42-6
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,47 @@ title: TO_TIMESTAMP
33
---
44
import FunctionDescription from '@site/src/components/FunctionDescription';
55

6-
<FunctionDescription description="Introduced or updated: v1.2.575"/>
6+
<FunctionDescription description="Introduced or updated: v1.2.664"/>
77

88
Converts an expression to a date with time.
99

1010
See also: [TO_DATE](to-date)
1111

1212
## Syntax
1313

14+
This function supports multiple overloads, covering the following use cases:
15+
1416
```sql
1517
-- Convert a string or integer to a timestamp
1618
TO_TIMESTAMP(<expr>)
1719
```
1820

19-
If given an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date format string, the function extracts a date from the string; If given is an integer, the function interprets the integer as the number of seconds, milliseconds, or microseconds before (for a negative number) or after (for a positive number) the Unix epoch (midnight on January 1, 1970):
21+
If given an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date format string, the function extracts a date from the string; If given is an integer, the function interprets the integer as the number of seconds, milliseconds, or microseconds before (for a negative number) or after (for a positive number) the Unix epoch (midnight on January 1, 1970), depending on the absolute value of `x`:
2022

2123
| Range | Unit |
2224
|---------------------------------------------|----------------------|
23-
| x < 31,536,000,000 | Seconds |
24-
| 31,536,000,000 ≤ x < 31,536,000,000,000 | Milliseconds |
25-
| x ≥ 31,536,000,000,000 | Microseconds |
25+
| \|x\| < 31,536,000,000 | Seconds |
26+
| 31,536,000,000 ≤ \|x\| < 31,536,000,000,000 | Milliseconds |
27+
| \|x\| ≥ 31,536,000,000,000 | Microseconds |
2628

2729
```sql
2830
-- Convert a string to a timestamp using the given pattern
2931
TO_TIMESTAMP(<expr>, <pattern>)
3032
```
3133

32-
If given two arguments, the function converts the first string to a timestamp based on the pattern specified in the second string. To specify the pattern, use specifiers. The specifiers allow you to define the desired format for date and time values. For a comprehensive list of supported specifiers, see [Formatting Date and Time](../../00-sql-reference/10-data-types/20-data-type-time-date-types.md#formatting-date-and-time).
34+
The function converts the first string to a timestamp based on the pattern specified in the second string. To specify the pattern, use specifiers. The specifiers allow you to define the desired format for date and time values. For a comprehensive list of supported specifiers, see [Formatting Date and Time](../../00-sql-reference/10-data-types/20-data-type-time-date-types.md#formatting-date-and-time).
35+
36+
37+
```sql
38+
-- Convert an integer to a timestamp based on the specified scale
39+
TO_TIMESTAMP(<int>, <scale>)
40+
```
41+
42+
The function converts an integer value to a timestamp, interpreting the integer as the number of seconds (or fractional seconds, based on the specified scale) since the Unix epoch (midnight on January 1, 1970). The scale defines the precision of the fractional seconds and supports values from 0 to 6. For example:
43+
44+
- `scale = 0`: Interprets the integer as seconds.
45+
- `scale = 1`: Interprets the integer as tenths of a second.
46+
- `scale = 6`: Interprets the integer as microseconds.
3347

3448
## Return Type
3549

@@ -181,4 +195,26 @@ SELECT TO_TIMESTAMP('2022/01/02T01:12:00', '%Y/%m/%dT%H:%M:%S');
181195
├──────────────────────────────────────────────────────────┤
182196
│ 2022-01-02 01:12:00 │
183197
└──────────────────────────────────────────────────────────┘
198+
```
199+
200+
### Example-4: Converting Integer with Scale
201+
202+
```sql
203+
-- Interpret an integer with seconds precision (scale = 0)
204+
SELECT TO_TIMESTAMP(1638473645, 0), TO_TIMESTAMP(-1638473645, 0);
205+
206+
┌─────────────────────────────────────────────────────────────┐
207+
│ to_timestamp(1638473645, 0) │ to_timestamp(- 1638473645, 0) │
208+
├─────────────────────────────┼───────────────────────────────┤
209+
│ 2021-12-02 19:34:05 │ 1918-01-30 04:25:55 │
210+
└─────────────────────────────────────────────────────────────┘
211+
212+
-- Interpret an integer with milliseconds precision (scale = 3)
213+
SELECT TO_TIMESTAMP(1638473645123, 3);
214+
215+
┌────────────────────────────────┐
216+
│ to_timestamp(1638473645123, 3) │
217+
├────────────────────────────────┤
218+
│ 2021-12-02 19:34:05.123 │
219+
└────────────────────────────────┘
184220
```

0 commit comments

Comments
 (0)