Skip to content

Date/Time Functions

Machbase DATETIME internally stores nanoseconds elapsed since 1970-01-01 00:00:00 UTC. Date/time functions convert these values to readable formats or perform arithmetic.

Quick Reference

FunctionSyntaxDescription
SYSDATE / NOWSYSDATE, NOWReturn current system time
TO_DATETO_DATE(str [, fmt])Convert a string to DATETIME
TO_DATE_SAFETO_DATE_SAFE(str [, fmt])Return NULL on conversion failure
TO_CHARTO_CHAR(col [, fmt])Convert DATETIME to a string
ADD_TIMEADD_TIME(col, diff)Add/subtract date/time components
DATE_TRUNCDATE_TRUNC(unit, col [, count])Truncate to the specified unit
DATE_BINDATE_BIN(unit, count, col [, origin])Bucket time relative to a specified origin
DAYOFWEEKDAYOFWEEK(col)Return weekday number (0=Sunday)
YEAR / MONTH / DAYYEAR(col), MONTH(col), DAY(col)Extract year, month, and day
FROM_UNIXTIMEFROM_UNIXTIME(unix_ts)Convert a 32-bit Unix timestamp to DATETIME
UNIX_TIMESTAMPUNIX_TIMESTAMP(col)Convert DATETIME to a 32-bit Unix timestamp
FROM_TIMESTAMPFROM_TIMESTAMP(ns)Convert nanosecond integer to DATETIME
TO_TIMESTAMPTO_TIMESTAMP(col)Convert DATETIME to nanosecond integer

SYSDATE / NOW

Pseudocolumns that return current system time. SYSDATE and NOW return the same value.

SYSDATE
NOW
Mach> SELECT SYSDATE, NOW FROM t1;
SYSDATE                         NOW
-------------------------------------------------------------------
2017-01-16 14:14:53 310:973:000 2017-01-16 14:14:53 310:973:000

TO_DATE

Converts a string to DATETIME using the specified format. The default format is YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn.

TO_DATE(date_string [, format_string])
Mach> SELECT TO_DATE('2014-12-30 11:22:33 444:555:666');
2014-12-30 11:22:33 444:555:666

Mach> SELECT TO_DATE('1999-12-31 13:12:32', 'YYYY-MM-DD HH24:MI:SS');
1999-12-31 13:12:32 000:000:000

Mach> SELECT TO_DATE('1999', 'YYYY');
1999-01-01 00:00:00 000:000:000

TO_DATE_SAFE() returns NULL instead of an error when conversion fails.

Mach> SELECT TO_DATE_SAFE('2016-12-32', 'YYYY-MM-DD');
NULL

TO_CHAR (DATETIME)

Converts a DATETIME column value to a string. The default format is YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn.

TO_CHAR(datetime_col [, format_string])

Format Strings

FormatDescription
YYYYFour-digit year
YYTwo-digit year
MMTwo-digit month (01~12)
MONThree-letter English month abbreviation (JAN, FEB, …)
DDTwo-digit day
DAYThree-letter English weekday abbreviation (SUN, MON, …)
IWISO 8601 week (1~53, Monday-based)
WWWeek of year (1~53, independent of weekday)
WWeek of month (1~5, independent of weekday)
HHTwo-digit hour
HH1212-hour clock (1~12)
HH2424-hour clock (0~23)
HH2, HH3, HH6Truncate hours to the specified multiple
MITwo-digit minute
MI2, MI5, MI10, MI20, MI30Truncate minutes to the specified multiple
SSTwo-digit second
SS2, SS5, SS10, SS20, SS30Truncate seconds to the specified multiple
AMAM/PM
mmmThree-digit milliseconds (0~999)
uuuThree-digit microseconds (0~999)
nnnThree-digit nanoseconds (0~999)
Mach> SELECT TO_CHAR(dt, 'YYYY-MM-DD HH24:MI:SS') FROM datetime_table;
2014-12-30 11:22:33
2013-11-11 01:02:03

Mach> SELECT TO_CHAR(dt, 'YYYY-MM-DD HH24:MI:SS mmm.uuu.nnn') FROM datetime_table;
2014-12-30 11:22:33 444.555.666

ADD_TIME

Adds or subtracts years, months, days, hours, minutes, and seconds from DATETIME. Milliseconds, microseconds, and nanoseconds are not supported.

ADD_TIME(column, time_diff_format)

time_diff_format: "Year/Month/Day Hour:Minute:Second"; each component can be positive or negative.

-- One year later
Mach> SELECT ADD_TIME(dt, '1/0/0 0:0:0') FROM t;

-- One hour, one minute, and one second later
Mach> SELECT ADD_TIME(dt, '0/0/0 1:1:1') FROM t;

-- One year, one month, and one day earlier
Mach> SELECT ADD_TIME(dt, '-1/-1/-1 0:0:0') FROM t;

DATE_TRUNC

Truncates a DATETIME value to the specified unit. With count, truncates to that multiple of the unit.

DATE_TRUNC(field, date_val [, count])

Supported Units and Maximum Ranges

UnitMaximum Range
nanosecond (nsec)1,000,000,000 (1 second)
microsecond (usec)60,000,000 (60 seconds)
millisecond (msec)60,000 (60 seconds)
second (sec)86,400 (1 day)
minute (min)1,440 (1 day)
hour24 (1 day)
day1
week1 (starts on Sunday)
month1
year1
-- Truncate to seconds
Mach> SELECT COUNT(*), DATE_TRUNC('second', i2) tm FROM t GROUP BY tm ORDER BY 2;

-- Truncate to 2-second intervals
Mach> SELECT COUNT(*), DATE_TRUNC('second', i2, 2) tm FROM t GROUP BY tm ORDER BY 2;

-- Truncate to 2-minute intervals (same as DATE_TRUNC('second', time, 120))
Mach> SELECT COUNT(*), DATE_TRUNC('minute', ts, 2) tm FROM t GROUP BY tm;

DATE_BIN

Buckets DATETIME by the specified unit and count relative to origin. If omitted, origin is 1970-01-01 00:00:00 in the local timezone.

DATE_BIN(field, count, source [, origin])
-- 2-hour buckets with a specific origin
SELECT DATE_BIN('hour', 2, time, TO_DATE('2020-01-01 00:00:00')) FROM log ORDER BY time;

-- 3-hour buckets aligned to the local timezone
SELECT DATE_BIN('hour', 3, ts) FROM t ORDER BY ts;

DAYOFWEEK

Returns the weekday of a DATETIME value as an integer.

DAYOFWEEK(date_val)
Return ValueWeekday
0Sunday
1Monday
2Tuesday
3Wednesday
4Thursday
5Friday
6Saturday
SELECT DAYOFWEEK(dt) FROM log_table;

YEAR / MONTH / DAY

Extracts year, month, and day from DATETIME as integers.

YEAR(datetime_col)
MONTH(datetime_col)
DAY(datetime_col)
Mach> SELECT YEAR(c1), MONTH(c1), DAY(c1) FROM extract_table;
year(c1)    month(c1)   day(c1)
---------------------------------
2001        1           1

FROM_UNIXTIME / UNIX_TIMESTAMP

FROM_UNIXTIME converts a 32-bit Unix timestamp integer to DATETIME. UNIX_TIMESTAMP converts DATETIME to a 32-bit Unix timestamp.

FROM_UNIXTIME(unix_timestamp_value)
UNIX_TIMESTAMP(datetime_value)
Mach> SELECT FROM_UNIXTIME(315540671);
1980-01-01 11:11:11 000:000:000

Mach> INSERT INTO unix_table VALUES (UNIX_TIMESTAMP('2001-01-01'));
Mach> SELECT * FROM unix_table;
C1
-----------
978274800

FROM_TIMESTAMP / TO_TIMESTAMP

FROM_TIMESTAMP converts an integer count of nanoseconds since 1970-01-01 00:00:00 UTC to DATETIME. TO_TIMESTAMP converts DATETIME to nanoseconds elapsed since the same epoch.

The epoch appears as 1970-01-01 09:00:00 in UTC+09:00. Dates and times in the examples below use UTC+09:00.

FROM_TIMESTAMP(nanosecond_time_value)
TO_TIMESTAMP(datetime_value)
Mach> SELECT FROM_TIMESTAMP(1562302560007248869);
2019-07-05 13:56:00 007:248:869

Mach> SELECT TO_TIMESTAMP(c1) FROM datetime_tbl;
to_timestamp(c1)
-----------------------
1262308210000000000

Nanosecond arithmetic example:

-- 1ms (1,000,000 ns) before the current time
SELECT FROM_TIMESTAMP(SYSDATE - 1000000) FROM t;
Last updated on