All Products
Search
Document Center

Time Series Database:TSQL functions

Last Updated:Mar 28, 2026

TSQL supports the following categories of built-in functions for querying time series data.

Aggregate functions

FunctionSupported data typesReturn typeDescription
avg(expression)SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLESame as inputAverage of the expression values
count(*)N/ABIGINTTotal number of rows
count(expression)All typesBIGINTNumber of non-null values
count(distinct expression)All typesN/ANumber of unique non-null values
max(expression)SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, VARCHARSame as inputMaximum value
min(expression)SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, VARCHARSame as inputMinimum value
ts_last(expression, timestamp)expression: DOUBLE, VARCHAR, BOOLEAN; timestamp: TIMESTAMPSame as expression typeValue at the maximum timestamp
ts_first(expression, timestamp)expression: DOUBLE, VARCHAR, BOOLEAN; timestamp: TIMESTAMPSame as expression typeValue at the minimum timestamp

ts_first and ts_last

ts_first and ts_last are time-series-specific aggregate functions. When working with time series data grouped by a time window, use these functions to retrieve the value at the earliest or latest timestamp within each group — useful for getting the opening or closing value of a metric in a window.

  • ts_first(expression, timestamp) returns the value of expression at the row with the smallest timestamp in the group.

  • ts_last(expression, timestamp) returns the value of expression at the row with the largest timestamp in the group.

Mathematical functions

Most mathematical functions accept input values of the following data types: INTEGER, BIGINT, FLOAT, DOUBLE, SMALLINT. See Trigonometric functions for the full list of supported trig functions, which follow the same input type rules.

FunctionReturn typeDescription
ABS(x)Same as inputAbsolute value of x
CBRT(x)FLOAT8Cube root of x
CEIL(x)Same as inputSmallest integer >= x
CEILING(x)Same as inputSmallest integer >= x (alias for CEIL)
DEGREES(x)FLOAT8Converts x from radians to degrees
E()FLOAT8Returns Euler's number: 2.718281828459045
EXP(x)FLOAT8e raised to the power of x
FLOOR(x)Same as inputLargest integer <= x
LOG(x)FLOAT8Natural logarithm (base e) of x
LOG(x, y)FLOAT8Logarithm of y to base x
LOG10(x)FLOAT8Base-10 logarithm of x
LSHIFT(x, y)Same as inputx left-shifted by y bits
MOD(x, y)FLOAT8Remainder of x divided by y
NEGATIVE(x)Same as inputNegation of x
PIFLOAT8Mathematical constant pi
POW(x, y)FLOAT8x raised to the power of y
RADIANS(x)FLOAT8Converts x from degrees to radians
RANDFLOAT8Random number in [0, 1)
ROUND(x)Same as inputx rounded to the nearest integer
RSHIFT(x, y)Same as inputx right-shifted by y bits
SIGN(x)INTSign of x: returns -1, 0, or 1
SQRT(x)Same as inputSquare root of x
TRUNC(x, y)DOUBLEx truncated to y decimal places (default: 0)

Examples

The following examples use the math_func_demo table:

SELECT * FROM math_func_demo;

+---------------+-----------------------+
|    integer    |         float         |
+---------------+-----------------------+
| 2010          | 17.4                  |
| -2002         | -1.2                  |
| 2001          | 1.2                   |
| 6005          | 1.2                   |
+---------------+-----------------------+

ABS

SELECT ABS(`integer`) FROM math_func_demo;

+------------+
|   EXPR$0   |
+------------+
| 2010       |
| 2002       |
| 2001       |
| 6005       |
+------------+
4 rows selected (0.357 seconds)

CEIL

SELECT CEIL(`float`) FROM math_func_demo;

+------------+
|   EXPR$0   |
+------------+
| 18.0       |
| -1.0       |
| 2.0        |
| 2.0        |
+------------+
4 rows selected (0.647 seconds)

FLOOR

SELECT FLOOR(`float`) FROM math_func_demo;

+------------+
|   EXPR$0   |
+------------+
| 17.0       |
| -2.0       |
| 1.0        |
| 1.0        |
+------------+
4 rows selected (0.11 seconds)

ROUND

ROUND(x) returns the nearest integer; ROUND(x, y) rounds to y decimal places.

SELECT ROUND(`float`) FROM math_func_demo;

+------------+
| EXPR$0     |
+------------+
| 3.0        |
| -1.0       |
| 1.0        |
| 1.0        |
+------------+
4 rows selected (0.061 seconds)

SELECT ROUND(`float`, 4) FROM math_func_demo;

+------------+
| EXPR$0     |
+------------+
| 3.1416     |
| -1.2       |
| 1.2        |
| 1.2        |
+------------+
4 rows selected (0.059 seconds)

LOG

LOG(x, y) returns the logarithm of y to base x. LOG10(x) uses base 10. LOG(x) is the natural logarithm.

SELECT LOG(2, 64) FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 6.0        |
+------------+
1 row selected (0.069 seconds)

SELECT LOG10(100) FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 2.0        |
+------------+
1 row selected (0.203 seconds)

SELECT LOG(7.5) FROM (VALUES(1));

+---------------------+
|       EXPR$0        |
+---------------------+
| 2.0149030205422647  |
+---------------------+
1 row selected (0.139 seconds)

Trigonometric functions

All trigonometric functions return FLOAT8. They accept the same input types as mathematical functions: INTEGER, BIGINT, FLOAT, DOUBLE, SMALLINT.

FunctionDescription
SIN(x)Sine of x
COS(x)Cosine of x
TAN(x)Tangent of x
ASIN(x)Arcsine of x
ACOS(x)Arccosine of x
ATAN(x)Arctangent of x
SINH(x)Hyperbolic sine of x
COSH(x)Hyperbolic cosine of x
TANH(x)Hyperbolic tangent of x

Examples

Use RADIANS to convert degrees before passing to a trig function:

SELECT RADIANS(30) AS Degrees FROM (VALUES(1));

+------------+
| Degrees    |
+------------+
| 0.7853981633974483 |
+------------+
1 row selected (0.045 seconds)

SELECT SIN(0.7853981633974483) AS `Sine of 30 degrees` FROM (VALUES(1));

+-----------------------+
| Sine of 45 degrees    |
+-----------------------+
| 0.7071067811865475    |
+-----------------------+
1 row selected (0.059 seconds)

SELECT TAN(0.7853981633974483) AS `Tangent of 30 degrees` FROM (VALUES(1));

+-----------------------+
| Tangent of 45 degrees |
+-----------------------+
| 0.9999999999999999    |
+-----------------------+

String functions

All string functions return VARCHAR unless noted otherwise.

FunctionReturn typeSyntax
CONCATVARCHARCONCAT(string [, string [, ...]])
INITCAPVARCHARINITCAP(string)
LENGTHINTEGERLENGTH(string [, encoding])
LOWERVARCHARLOWER(string)
LPADVARCHARLPAD(string, length [, fill text])
LTRIMVARCHARLTRIM(string1, string2)
REGEXP_REPLACEVARCHARREGEXP_REPLACE(source_char, pattern, replacement)
RPADVARCHARRPAD(string, length [, fill text])
RTRIMVARCHARRTRIM(string1, string2)
STRPOSINTEGERSTRPOS(string, substring)
SUBSTRVARCHARSUBSTR(string, x [, y])
TRIMVARCHARTRIM([leading | trailing | both] [string1] FROM string2)
UPPERVARCHARUPPER(string)

CONCAT

Concatenates two or more strings.

SELECT CONCAT('Drill', ' ', 1.0, ' ', 'release') FROM (VALUES(1));

+--------------------+
|       EXPR$0       |
+--------------------+
| Drill 1.0 release  |
+--------------------+
1 row selected (0.134 seconds)

INITCAP

Returns the string with the first letter of each word in uppercase and all remaining letters in lowercase.

SELECT INITCAP('china beijing') FROM (VALUES(1));

+---------------------------+
|          EXPR$0           |
+---------------------------+
|ChinaBeijing|
+---------------------------+
1 row selected (0.106 seconds)

LENGTH

Returns the number of characters in a string.

SELECT LENGTH('Hangzhou') FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 8          |
+------------+
1 row selected (0.127 seconds)

LOWER and UPPER

LOWER converts all characters to lowercase; UPPER converts all to uppercase.

SELECT LOWER('China Beijing') FROM (VALUES(1));

+---------------+
|    EXPR$0     |
+---------------+
| china beijing |
+---------------+
1 row selected (0.103 seconds)

SELECT UPPER('china beijing') FROM (VALUES(1));

+---------------+
|    EXPR$0     |
+---------------+
| CHINA BEIJING |
+---------------+
1 row selected (0.081 seconds)

LPAD

Left-pads string to length using fill text (default: space). If the result exceeds length, it is truncated.

SELECT LPAD('hi', 5, 'xy') FROM (VALUES(1));

+------------------------------+
|            EXPR$0            |
+------------------------------+
| xyxhi                        |
+------------------------------+
1 row selected (0.132 seconds)

RPAD

Right-pads string to length using fill text (default: space). If the result exceeds length, it is truncated.

SELECT RPAD('hi', 5, 'xy') FROM (VALUES(1));

+-------------------------+
|         EXPR$0          |
+-------------------------+
| hixyx                   |
+-------------------------+
1 row selected (0.107 seconds)

LTRIM and RTRIM

LTRIM(string1, string2) removes characters in string2 from the left of string1. RTRIM does the same from the right. Related functions: TRIM.

SELECT LTRIM('zzzytest', 'xyz') FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| test       |
+------------+
1 row selected (0.131 seconds)

SELECT RTRIM('testxxzx', 'xyz') FROM (VALUES(1));

+--------------------+
|       EXPR$0       |
+--------------------+
| tes                |
+--------------------+
1 row selected (0.102 seconds)

REGEXP_REPLACE

Searches source_char for each occurrence of the Java regular expression pattern and replaces it with replacement.

SELECT REGEXP_REPLACE('abc, acd, ade, aef', 'a', 'b') FROM (VALUES(1));

+---------------------+
|       EXPR$0        |
+---------------------+
| bbc, bcd, bde, bef  |
+---------------------+
1 row selected (0.105 seconds)

SELECT REGEXP_REPLACE('abc, acd, ade, aef', 'a.', 'b') FROM (VALUES(1));

+-----------------+
|     EXPR$0      |
+-----------------+
| bc, bd, be, bf  |
+-----------------+
1 row selected (0.113 seconds)

STRPOS

Returns the position of substring within string. Positions are 1-indexed.

SELECT STRPOS('high', 'ig') FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 2          |
+------------+
1 row selected (0.22 seconds)

SUBSTR

Returns a substring starting at index x. The optional y parameter specifies the end index.

SELECT SUBSTR('China Beijing', 7) FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| Beijing    |
+------------+
1 row selected (0.134 seconds)

SELECT SUBSTR('China Beijing', 3, 2) FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| in         |
+------------+
1 row selected (0.129 seconds)

TRIM

Removes string1 characters from the leading, trailing, or both sides of string2. Related functions: LTRIM, RTRIM.

SELECT TRIM(trailing 'A' FROM 'AABBAA') FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| AABB       |
+------------+
1 row selected (0.172 seconds)

SELECT TRIM(both 'A' FROM 'AABBAA') FROM (VALUES(1));

+---------------+
|    EXPR$0     |
+---------------+
| BB            |
+---------------+
1 row selected (0.104 seconds)

SELECT TRIM(leading 'A' FROM 'AABBAA') FROM (VALUES(1));

+-----------------+
|     EXPR$0      |
+-----------------+
| BBAA            |
+-----------------+
1 row selected (0.101 seconds)

Timestamp functions

FunctionReturn typeDescriptionExample
now()TIMESTAMPCurrent timestampnow()
CURRENT_TIMESTAMPTIMESTAMPCurrent timestampCURRENT_TIMESTAMP
CURRENT_DATEDATECurrent dateCURRENT_DATE
CURRENT_TIMETIMECurrent time (no date component)CURRENT_TIME
EXTRACT(component FROM timestamp/date/time)INTEGERExtracts year, month, day, hour, minute, or secondEXTRACT(day FROM 'timestamp')
tumble(timestamp, interval)TIMESTAMPLower boundary of the tumbling window containing timestamptumble('timestamp', interval '5' minute)
date_diff(timestamp, interval)TIMESTAMPNew timestamp after subtracting intervaldate_diff(timestamp, interval '5' minute)
date_add(timestamp, interval)TIMESTAMPNew timestamp after adding intervaldate_add(timestamp, interval '5' minute)

tumble

tumble(timestamp, interval) maps a timestamp to the start of the fixed-size time window that contains it. Use this function to downsample time series data by grouping rows into non-overlapping time windows and applying an aggregate function to each window.

For example, tumble(TIMESTAMP '2001-02-16 20:38:40', interval '5' minute) returns 2001-02-16 20:35:00.0 — the start of the 5-minute window that contains 20:38:40.

Examples

SELECT CURRENT_DATE FROM (VALUES(1));

+---------------+
| CURRENT_DATE  |
+---------------+
| 2019-11-27    |
+---------------+


SELECT EXTRACT(hour FROM TIME '17:12:28.5') FROM (VALUES(1));

+---------+
| EXPR$0  |
+---------+
| 17      |
+---------+


SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40') FROM (VALUES(1));

+---------+
| EXPR$0  |
+---------+
| 40.0    |
+---------+


SELECT DATE_DIFF(TIMESTAMP '2001-02-16 20:38:40', interval '5' minute) FROM (VALUES(1));

+------------------------+
|         EXPR$0         |
+------------------------+
| 2001-02-16 20:33:40.0  |
+------------------------+


SELECT DATE_ADD(TIMESTAMP '2001-02-16 20:38:40', interval '5' minute) FROM (VALUES(1));

+------------------------+
|         EXPR$0         |
+------------------------+
| 2001-02-16 20:43:40.0  |
+------------------------+


SELECT tumble(TIMESTAMP '2001-02-16 20:38:40', interval '5' minute) FROM (VALUES(1));

+------------------------+
|         EXPR$0         |
+------------------------+
| 2001-02-16 20:35:00.0  |
+------------------------+

Data type conversion functions

CAST

Converts an expression from one data type to another.

Syntax

CAST (<expression> AS <data type>)

Parameters

ParameterDescription
expressionA value, column reference, or combination of operators and functions to evaluate
data typeThe target data type, such as INTEGER or DATE

Examples

SELECT CAST(456 AS VARCHAR(3)) FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 456        |
+------------+
1 row selected (0.08 seconds)

SELECT CAST(456 AS CHAR(3)) FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 456        |
+------------+
1 row selected (0.093 seconds)

Timestamp, date, and time conversion functions

TSQL natively recognizes the following date and time formats:

  • 2008-12-15

  • 22:55:55.123...

For other formats, use the following conversion functions:

FunctionReturn typeDescription
TO_CHAR(expression, format)VARCHARConverts a timestamp, date, time, or number to a string
TO_DATE(expression, format)DATEConverts a string or UNIX timestamp (in milliseconds) to a date
TO_TIME(expression)TIMEConverts a UNIX timestamp (in milliseconds) to a time
TO_TIMESTAMP(VARCHAR, format)TIMESTAMPConverts a formatted string to a timestamp
TO_TIMESTAMP(DOUBLE)TIMESTAMPConverts a UNIX timestamp (in seconds) to a timestamp

Joda-Time format specifiers

The format parameter in TO_CHAR, TO_DATE, and TO_TIMESTAMP uses Joda-Time symbols:

SymbolMeaningPresentationExample
GEraTextAD
CCentury of era (>=0)Number20
YYear of era (>=0)Year1996
xWeek yearYear1996
wWeek of week yearNumber27
eDay of weekNumber2
EDay of weekTextTuesday; Tue
yYearYear1996
DDay of yearNumber189
MMonth of yearMonthJuly; Jul; 07
dDay of monthNumber10
aHalf-dayTextPM
KHour of half-day (0-11)Number0
hClock hour of half-day (1-12)Number12
HHour of day (0-23)Number0
kClock hour of day (1-24)Number24
mMinute of hourNumber30
sSecond of minuteNumber55
SFraction of secondNumber978
zTime zoneTextPacific Standard Time; PST
ZTime zone offset/IDZone-0800; -08:00; America/Los_Angeles
'Escape for text delimiterLiteral-

TO_CHAR examples

SELECT TO_CHAR(1256.789383, '#,###.###') FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 1,256.789  |
+------------+
1 row selected (1.767 seconds)

SELECT TO_CHAR(125677.4567, '#,###.###') FROM (VALUES(1));

+--------------+
|    EXPR$0    |
+--------------+
| 125,677.457  |
+--------------+
1 row selected (0.083 seconds)

SELECT TO_CHAR((CAST('2008-2-23' AS DATE)), 'yyyy-MMM-dd') FROM (VALUES(1));

+--------------+
|    EXPR$0    |
+--------------+
| 2008-Feb-23  |
+--------------+
1 row selected (0.166 seconds)

SELECT TO_CHAR(CAST('12:20:30' AS TIME), 'HH mm ss') FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
|122030|
+------------+
1 row selected (0.07 seconds)

SELECT TO_CHAR(CAST('2015-2-23 12:00:00' AS TIMESTAMP), 'yyyy MMM dd HH:mm:ss') FROM (VALUES(1));

+-----------------------+
|        EXPR$0         |
+-----------------------+
|2015Feb2312:00:00|
+-----------------------+
1 row selected (0.142 seconds)

TO_DATE examples

-- Convert a string to a date
SELECT TO_DATE('2015-FEB-23', 'yyyy-MMM-dd') FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 2015-02-23 |
+------------+
1 row selected (0.077 seconds)

-- Extract year from a converted date
SELECT EXTRACT(year FROM mydate) AS `extracted year`
FROM (SELECT TO_DATE('2015-FEB-23', 'yyyy-MMM-dd') AS mydate FROM (VALUES(1)));

+----------------+
| extracted year |
+----------------+
| 2015           |
+----------------+
1 row selected (0.128 seconds)

-- Convert a UNIX timestamp (milliseconds) to a date
SELECT TO_DATE(1427849046000) FROM (VALUES(1));

+------------+
|   EXPR$0   |
+------------+
| 2015-04-01 |
+------------+
1 row selected (0.082 seconds)

TO_TIME example

-- Convert a UNIX timestamp (milliseconds) to a time
SELECT to_time(82855000) FROM (VALUES(1));

+------------+
| EXPR$0     |
+------------+
| 23:00:55   |
+------------+
1 row selected (0.086 seconds)

TO_TIMESTAMP examples

-- Convert a formatted string to a timestamp
SELECT TO_TIMESTAMP('2008-2-23 12:00:00', 'yyyy-MM-dd HH:mm:ss') FROM (VALUES(1));

+------------------------+
|         EXPR$0         |
+------------------------+
| 2008-02-23 12:00:00.0  |
+------------------------+
1 row selected (0.126 seconds)

-- Convert a UNIX timestamp (seconds) to a timestamp
SELECT TO_TIMESTAMP(1427936330) FROM (VALUES(1));

+------------------------+
|         EXPR$0         |
+------------------------+
| 2015-04-01 17:58:50.0  |
+------------------------+
1 row selected (0.114 seconds)

-- Convert a UTC string to a timestamp and extract the time zone
SELECT TO_TIMESTAMP('2015-03-30 20:49:59.0 UTC', 'YYYY-MM-dd HH:mm:ss.s z') AS Original,
       TO_CHAR(TO_TIMESTAMP('2015-03-30 20:49:59.0 UTC', 'YYYY-MM-dd HH:mm:ss.s z'), 'z') AS New_TZ
FROM (VALUES(1));

+------------------------+---------+
|        Original        | New_TZ  |
+------------------------+---------+
| 2015-03-30 20:49:00.0  | UTC     |
+------------------------+---------+
1 row selected (0.148 seconds)