LindormTable supports four time functions for formatting timestamps, converting between UNIX timestamps and session-local timestamps, and calculating date differences. These functions are applicable only to LindormTable. All functions run in the time zone of the current session.
Prerequisites
Before you begin, ensure that you have:
LindormTable 2.7.8 or later (for DATE_FORMAT, FROM_UNIXTIME, and DATEDIFF)
Lindorm SQL 2.8.7.0 or later (for DATE_FORMAT, FROM_UNIXTIME, and DATEDIFF)
LindormTable 2.8.0 or later and Lindorm SQL 2.9.0.0 or later (for UNIX_TIMESTAMP)
To view or upgrade the LindormTable minor engine version, see Release notes of LindormTable and Upgrade the minor engine version of a Lindorm instance. To view the Lindorm SQL version, see SQL versions.
Supported functions
| Function | Description |
|---|---|
| DATE_FORMAT | Formats a timestamp using the time zone of the current session. |
| FROM_UNIXTIME | Converts a Number-type value (BIGINT, INTEGER, SMALLINT, or TINYINT) to a timestamp using the time zone of the current session. The inverse of UNIX_TIMESTAMP. |
| DATEDIFF | Returns the difference in days between two timestamps, based on date values only. |
| UNIX_TIMESTAMP | Converts a timestamp or timestamp string to a UNIX timestamp. Returns a DOUBLE value in seconds. The inverse of FROM_UNIXTIME. |
DATE_FORMAT
Formats a timestamp using the time zone of the current session. To view or set the session time zone, see Specify a time zone for a LindormTable connection.
Syntax
DATE_FORMAT(TIMESTAMP ts, STRING format)Parameters
| Parameter | Required | Description |
|---|---|---|
ts | Yes | The timestamp to format. |
format | Yes | A format string composed of format specifiers (for example, %Y-%m-%d). Characters not starting with % are returned as-is. For the full list of supported specifiers, see Format specifiers. |
Example
Format the timestamp 2024-01-01 17:20:35 as at 17:20:35 on Jan 1st, 2024:
SELECT DATE_FORMAT('2024-01-01 17:20:35', 'at %T on %b %D, %Y');Output:
+------------------------------+
| EXPR$0 |
+------------------------------+
| at 17:20:35 on JAN 1st, 2024 |
+------------------------------+FROM_UNIXTIME
Converts a Number-type value to a timestamp using the time zone of the current session. This is the inverse of UNIX_TIMESTAMP. Accepts BIGINT, INTEGER, SMALLINT, and TINYINT values. The input represents seconds elapsed since 1970-01-01 00:00:00 UTC, with up to three decimal places retained.
Only the MySQL protocol supports changing the time zone of a connection. See Use the MySQL protocol to develop applications (recommended) and Use a MySQL client to connect to and use LindormTable.
Syntax
-- Convert a UNIX timestamp to a session-local timestamp.
FROM_UNIXTIME(Number seconds)
-- Convert and format the result.
-- Equivalent to: DATE_FORMAT(FROM_UNIXTIME(seconds), format)
FROM_UNIXTIME(Number seconds, STRING format)The two-parameter form is equivalent to calling DATE_FORMAT on the result of the one-parameter form. The format parameter accepts the same specifiers as DATE_FORMAT. For details, see Format specifiers.
Parameters
| Parameter | Required | Description |
|---|---|---|
seconds | Yes | A UNIX timestamp in seconds. Up to three decimal places are retained. The value represents seconds elapsed since 1970-01-01 00:00:00 UTC. |
format | No | A format string. Uses the same specifiers as DATE_FORMAT. |
Examples
Example 1: Convert a UNIX timestamp
Convert 10.1578 to a timestamp using the time zone of the current session:
SELECT FROM_UNIXTIME(10.1578);Output:
+-------------------------+
| EXPR$0 |
+-------------------------+
| 1970-01-01 08:00:10.158 |
+-------------------------+Change the session time zone to +03:00 and convert again:
-- Change the session time zone.
SET @@time_zone='+03:00';
-- Convert using the new time zone.
SELECT FROM_UNIXTIME(10.1578);Output:
+-------------------------+
| EXPR$0 |
+-------------------------+
| 1970-01-01 03:00:10.158 |
+-------------------------+Example 2: Convert and format
Convert 10.1578 and format the result using the time zone of the current session:
SELECT FROM_UNIXTIME(10.1578, 'at %T on %b %D, %Y');Output:
+------------------------------+
| EXPR$0 |
+------------------------------+
| at 08:00:10 on JAN 1st, 1970 |
+------------------------------+Change the session time zone to +03:00 and format again:
-- Change the session time zone.
SET @@time_zone='+03:00';
-- Convert and format using the new time zone.
SELECT FROM_UNIXTIME(10.1578, 'at %T on %b %D, %Y');Output:
+------------------------------+
| EXPR$0 |
+------------------------------+
| at 03:00:10 on JAN 1st, 1970 |
+------------------------------+DATEDIFF
Returns the difference in days between two timestamps in the time zone of the current session. The result equals ts1 - ts2, comparing date values only — hours, minutes, and seconds are ignored.
Positive result:
ts1is a later date thants2.Negative result:
ts1is an earlier date thants2.Zero:
ts1andts2fall on the same date.
Syntax
DATEDIFF(TIMESTAMP ts1, TIMESTAMP ts2)Parameters
| Parameter | Required | Description |
|---|---|---|
ts1 | Yes | The first timestamp. |
ts2 | Yes | The second timestamp. |
Examples
Example 1: ts1 is earlier than ts2
SELECT DATEDIFF('2024-01-01 15:30:00', '2024-01-02 00:59:59');Output:
+--------+
| EXPR$0 |
+--------+
| -1 |
+--------+Example 2: ts1 is later than ts2
SELECT DATEDIFF('2024-01-05 15:30:00', '2024-01-02 00:59:59');Output:
+--------+
| EXPR$0 |
+--------+
| 3 |
+--------+Example 3: ts1 and ts2 fall on the same date
Time-of-day values differ, but the date is the same, so the result is 0:
SELECT DATEDIFF('2024-01-02 15:30:00', '2024-01-02 00:59:59');Output:
+--------+
| EXPR$0 |
+--------+
| 0 |
+--------+UNIX_TIMESTAMP
Converts a timestamp or timestamp string to a UNIX timestamp. Returns a DOUBLE value. This is the inverse of FROM_UNIXTIME.
UNIX_TIMESTAMP requires LindormTable 2.8.0 or later and Lindorm SQL 2.9.0.0 or later. Upgrade the LindormTable minor engine version in the Lindorm console if needed.
The precision of the returned value depends on the input type:
TIMESTAMP input: 3 decimal places retained.
VARCHAR input: The timestamp string represents the timestamp in the time zone of the current session. Up to 6 decimal places are retained.
UNIX_TIMESTAMP can be used in:
The projection (column list) of a SELECT statement
The WHERE clause of a SELECT statement
The VALUES clause of an UPSERT statement
Syntax
UNIX_TIMESTAMP(ts)Parameters
| Parameter | Required | Description |
|---|---|---|
ts | Yes | The timestamp to convert. Must be TIMESTAMP or VARCHAR. |
Examples
Create a table and insert sample data:
-- Create a table with TIMESTAMP and VARCHAR timestamp columns.
CREATE TABLE t1(p1 INT, c1 TIMESTAMP, c2 VARCHAR, c3 DOUBLE, PRIMARY KEY(p1));
-- Insert sample rows.
UPSERT INTO t1(p1, c1, c2, c3) VALUES
(1, '2025-01-01 08:30:00', '2025-01-01 08:30:00', '12.24'),
(2, '1970-01-01 08:00:00.123', '1970-01-01 08:00:00.123', '10.32'),
(3, '1970-01-01 08:00:10.13579', '1970-01-01 08:00:10.13579', '10.12'),
(4, '1970-01-01 08:00:10.123456789','1970-01-01 08:00:10.123456789','17.33');Example 1: Query UNIX timestamps from a table
TIMESTAMP values (c1) retain 3 decimal places; VARCHAR values (c2) retain up to 6.
SELECT p1, UNIX_TIMESTAMP(c1), UNIX_TIMESTAMP(c2) FROM t1;Output:
+----+------------+------------+
| p1 | EXPR$1 | EXPR$2 |
+----+------------+------------+
| 1 | 1735691400 | 1735691400 |
| 2 | 0.123 | 0.123 |
| 3 | 10.135 | 10.13579 |
| 4 | 10.123 | 10.123457 |
+----+------------+------------+Example 2: Filter rows using UNIX_TIMESTAMP in a WHERE clause
Count rows where the UNIX timestamp of c1 exceeds 10:
SELECT COUNT(*) FROM t1 WHERE UNIX_TIMESTAMP(c1) > 10;Output:
+--------+
| EXPR$0 |
+--------+
| 3 |
+--------+Example 3: Write a UNIX_TIMESTAMP result to a DOUBLE column
Use UNIX_TIMESTAMP in a VALUES clause to write the result directly to the c3 DOUBLE column:
UPSERT INTO t1(p1, c3) VALUES(1, UNIX_TIMESTAMP('1970-01-01 08:00:00.123'));To verify the result, run:
SELECT * FROM t1;Format specifiers
The following format specifiers are supported by DATE_FORMAT and FROM_UNIXTIME.
Characters in the format string that do not start with % are returned as-is.
| Specifier | Output | Description |
|---|---|---|
%a | Sun to Sat | Abbreviated weekday name. |
%b | Jan to Dec | Abbreviated month name. |
%c | 0 to 12 | Month as a numeric value. |
%D | 1st, 2nd, 3rd... | Day of the month with ordinal suffix. |
%d | 00 to 31 | Day of the month, two digits. |
%e | 00 to 31 | Day of the month, two digits. |
%f | 000000 to 999999 | Microseconds. |
%H | 00 to 23 | Hour in 24-hour clock, two digits. |
%h | 01 to 12 | Hour in 12-hour clock, two digits. |
%I | 01 to 12 | Hour in 12-hour clock, two digits. |
%i | 00 to 59 | Minutes, two digits. |
%j | 001 to 366 | Day of the year, three digits. |
%k | 0 to 23 | Hour in 24-hour clock, numeric. |
%l | 1 to 12 | Hour in 12-hour clock, numeric. |
%M | January to December | Full month name. |
%m | 01 to 12 | Month, two digits. |
%p | AM or PM | AM/PM indicator. |
%r | hh:mm:ss AM/PM | Time in 12-hour clock format. |
%S | 00 to 59 | Seconds, two digits. |
%s | 00 to 59 | Seconds, two digits. |
%T | hh:mm:ss | Time in 24-hour clock format. |
%W | Sunday to Saturday | Full weekday name. |
%w | 0 to 6 | Day of the week as a number. 0 = Sunday, 1 = Monday. |
%Y | Four-digit year | Example: 2025. |
%y | Two-digit year | Based on the last two digits of the year. Example: 24 for 2024. |
%% | % | A literal percent sign. |
%x | x | Any letter that is not a reserved specifier, returned as-is. Example: %U returns U. |