Functions
Single Row Functions
Single row functions return a result row for each row in the query results.
Mathematical Functions
ABS
ABS(expr)
Function Description: Gets the absolute value of the specified field.
Return Type: Consistent with the original data type of the specified field.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes: Can only be used with normal columns, selection, and projection functions, not with aggregation functions.
ACOS
ACOS(expr)
Function Description: Gets the arccosine of the specified field.
Return Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes: Can only be used with normal columns, selection, and projection functions, not with aggregation functions.
ASIN
ASIN(expr)
Function Description: Gets the arcsine of the specified field.
Return Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes: Can only be used with normal columns, selection, and projection functions, not with aggregation functions.
ATAN
ATAN(expr)
Function Description: Gets the arctangent of the specified field.
Return Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes: Can only be used with normal columns, selection, and projection functions, not with aggregation functions.
CEIL
CEIL(expr)
Function Description: Gets the ceiling of the specified field.
Return Type: Consistent with the original data type of the specified field.
Applicable Data Types: Numeric types.
Applicable to: Tables and supertables.
Nested Subquery Support: Applicable to both inner and outer queries.
Usage Notes: Can only be used with normal columns, selection, and projection functions, not with aggregation functions.
COS
COS(expr)
Function Description: Gets the cosine of the specified field.
Return Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes: Can only be used with normal columns, selection, and projection functions, not with aggregation functions.
FLOOR
FLOOR(expr)
Function Description: Gets the floor of the specified field. Other usage notes see CEIL function description.
LOG
LOG(expr1[, expr2])
Function Description: Gets the logarithm of expr1 to the base expr2. If the expr2 parameter is omitted, it returns the natural logarithm of the specified field.
Return Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes: Can only be used with normal columns, selection, and projection functions, not with aggregation functions.
POW
POW(expr1, expr2)
Function Description: Gets the power of expr1 raised to the exponent expr2.
Return Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions: Can only be used with regular columns, selection (Selection), projection (Projection) functions, and cannot be used with aggregation (Aggregation) functions.
ROUND
ROUND(expr[, digits])
Function Description: Obtains the rounded result of the specified field.
Return Result Type: Consistent with the original data type of the specified field.
Applicable Data Types:
expr
: Numeric type.digits
: Numeric type.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
expr
ordigits
is NULL, returns NULL. - If
digits
is specified, it retainsdigits
decimal places, default is 0. - If the input value is of INTEGER type, regardless of the value of
digits
, it will only return INTEGER type, without retaining decimals. digits
greater than zero means operating on the decimal places, rounding todigits
decimal places. If the number of decimal places is less thandigits
, no rounding operation is performed, and it is returned directly.digits
less than zero means discarding the decimal places and rounding the number to the left of the decimal point bydigits
places. If the number of places to the left of the decimal point is less thandigits
, returns 0.- Since the DECIMAL type is not yet supported, this function will use DOUBLE and FLOAT to represent results containing decimals, but DOUBLE and FLOAT have precision limits, and using this function may be meaningless when there are too many digits.
- Can only be used with regular columns, selection (Selection), projection (Projection) functions, and cannot be used with aggregation (Aggregation) functions.
digits
is supported from version 3.3.3.0.
Example:
taos> select round(8888.88);
round(8888.88) |
============================
8889.000000000000000 |
taos> select round(8888.88,-1);
round(8888.88,-1) |
============================
8890.000000000000000 |
SIN
SIN(expr)
Function Description: Obtains the sine result of the specified field.
Return Result Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions: Can only be used with regular columns, selection (Selection), projection (Projection) functions, and cannot be used with aggregation (Aggregation) functions.
SQRT
SQRT(expr)
Function Description: Obtains the square root of the specified field.
Return Result Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions: Can only be used with regular columns, selection (Selection), projection (Projection) functions, and cannot be used with aggregation (Aggregation) functions.
TAN
TAN(expr)
Function Description: Obtains the tangent result of the specified field.
Version: ver-3.3.3.0
Return Result Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions: Can only be used with regular columns, selection (Selection), projection (Projection) functions, and cannot be used with aggregation (Aggregation) functions.
PI
PI()
Function Description: Returns the value of π (pi).
Return Result Type: DOUBLE.
Applicable Data Types: None.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- π ≈ 3.141592653589793.
- Can only be used with regular columns, selection (Selection), projection (Projection) functions, and cannot be used with aggregation (Aggregation) functions.
Example:
taos> select pi();
pi() |
============================
3.141592653589793 |
TRUNCATE
TRUNCATE(expr, digits)
Function Description: Gets the truncated value of the specified field to the specified number of digits.
Version: ver-3.3.3.0
Return Type: Consistent with the original data type of the expr
field.
Applicable Data Types:
expr
: Numeric type.digits
: Numeric type.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
expr
ordigits
is NULL, return NULL. - Truncation is done directly to the specified number of digits without rounding.
digits
greater than zero means operating on the decimal places, truncating todigits
decimal places. If the number of decimal places is less thandigits
, no truncation is performed, and the value is returned directly.digits
equal to zero means dropping the decimal places.digits
less than zero means dropping the decimal places and zeroing the positions to the left of the decimal point up todigits
. If the number of positions to the left of the decimal point is less thandigits
, return 0.- Since the DECIMAL type is not yet supported, this function uses DOUBLE and FLOAT to represent results containing decimals, but DOUBLE and FLOAT have precision limits, and using this function may be meaningless when the number of digits is too large.
- Can only be used with regular columns, selection, and projection functions, not with aggregation functions.
Example:
taos> select truncate(8888.88, 0);
truncate(8888.88, 0) |
============================
8888.000000000000000 |
taos> select truncate(8888.88, -1);
truncate(8888.88, -1) |
============================
8880.000000000000000 |
EXP
EXP(expr)
Function Description: Returns the value of e (the base of natural logarithms) raised to the specified power.
Version: ver-3.3.3.0
Return Type: DOUBLE.
Applicable Data Types: Numeric type.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
expr
is NULL, return NULL. - Can only be used with regular columns, selection, and projection functions, not with aggregation functions.
Example:
taos> select exp(2);
exp(2) |
============================
7.389056098930650 |
LN
LN(expr)
Function Description: Returns the natural logarithm of the specified parameter.
Version: ver-3.3.3.0
Return Type: DOUBLE.
Applicable Data Types: Numeric type.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
expr
is NULL, return NULL. - If
expr
is less than or equal to 0, return NULL. - Can only be used with regular columns, selection, and projection functions, not with aggregation functions.
Example:
taos> select ln(10);
ln(10) |
============================
2.302585092994046 |
MOD
MOD(expr1, expr2)
Function Description: Calculates the result of expr1 % expr2.
Version: ver-3.3.3.0
Return Type: DOUBLE.
Applicable Data Types: Numeric type.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- Returns NULL if
expr2
is 0. - Returns NULL if
expr1
orexpr2
is NULL. - Can only be used with regular columns, selection (Selection), and projection (Projection) functions, not with aggregation (Aggregation) functions.
Example:
taos> select mod(10,3);
mod(10,3) |
============================
1.000000000000000 |
taos> select mod(1,0);
mod(1,0) |
============================
NULL |
RAND
RAND([seed])
Function Description: Returns a uniformly distributed random number from 0 to 1.
Version: ver-3.3.3.0
Return Result Type: DOUBLE.
Applicable Data Types:
seed
: INTEGER.
Nested Subquery Support: Applicable to inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If a
seed
value is specified, it will use the specifiedseed
as the random seed to ensure the generated random number sequence is deterministic. - Can only be used with regular columns, selection (Selection), and projection (Projection) functions, not with aggregation (Aggregation) functions.
Example:
taos> select rand();
rand() |
============================
0.202092426923147 |
taos> select rand();
rand() |
============================
0.131537788143166 |
taos> select rand(1);
rand(1) |
============================
0.000007826369259 |
taos> select rand(1);
rand(1) |
============================
0.000007826369259 |
SIGN
SIGN(expr)
Function Description: Returns the sign of the specified parameter.
Version: ver-3.3.3.0
Return Result Type: Consistent with the original data type of the specified field.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- Returns -1 if
expr
is negative, - Returns 1 if
expr
is positive, - Returns 0 if
expr
is 0, - Returns NULL if
expr
is NULL, - Can only be used with regular columns, selection (Selection), and projection (Projection) functions, not with aggregation (Aggregation) functions.
Example:
taos> select sign(-1);
sign(-1) |
========================
-1 |
taos> select sign(1);
sign(1) |
========================
1 |
taos> select sign(0);
sign(0) |
========================
0 |
DEGREES
DEGREES(expr)
Function Description: Calculates the value of the specified parameter converted from radians to degrees.
Version: ver-3.3.3.0
Return Result Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to inner and outer queries.
Applicable to: Tables and supertables.
Usage:
- If
expr
is NULL, it returns NULL. - degree = radian * 180 / π.
- Can only be used with regular columns, selection (Selection), and projection (Projection) functions, not with aggregation (Aggregation) functions.
Example:
taos> select degrees(PI());
degrees(pi()) |
============================
180.000000000000000 |
RADIANS
RADIANS(expr)
Function Description: Calculates the value of the specified parameter converted from degrees to radians.
Version: ver-3.3.3.0
Return Type: DOUBLE.
Applicable Data Types: Numeric types.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage:
- If
expr
is NULL, it returns NULL. - radian = degree * π / 180.
- Can only be used with regular columns, selection (Selection), and projection (Projection) functions, not with aggregation (Aggregation) functions.
Example:
taos> select radians(180);
radians(180) |
============================
3.141592653589793 |
String Functions
The input parameters for string functions are of string type, and the return results are of numeric type or string type.
CHAR_LENGTH
CHAR_LENGTH(expr)
Function Description: String length counted in characters.
Return Type: BIGINT.
Applicable Data Types: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage:
- Unlike the
LENGTH()
function, for multibyte characters, such as Chinese characters, theCHAR_LENGTH()
function counts them as one character, length 1, whileLENGTH()
calculates their byte count, length 3. For example,CHAR_LENGTH('你好') = 2
,LENGTH('你好') = 6
. - If
expr
is NULL, it returns NULL.
Example:
taos> select char_length('Hello world');
char_length('Hello world') |
=============================
11 |
taos> select char_length('你好 世界');
char_length('你好 世界') |
===============================
5 |
CONCAT
CONCAT(expr1, expr2 [, expr] ... )
Function Description: String concatenation function.
Return Type: If all parameters are of VARCHAR type, the result type is VARCHAR. If parameters include NCHAR type, the result type is NCHAR. If parameters include NULL values, the output is NULL.
Applicable Data Types: VARCHAR, NCHAR. The function requires a minimum of 2 parameters and a maximum of 8 parameters.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
CONCAT_WS
CONCAT_WS(separator_expr, expr1, expr2 [, expr] ... )
Function Description: String concatenation function with a separator.
Return Type: If all parameters are of VARCHAR type, the result type is VARCHAR. If parameters include NCHAR type, the result type is NCHAR. If parameters include NULL values, the output is NULL.
Applicable Data Types: VARCHAR, NCHAR. The function requires a minimum of 3 parameters and a maximum of 9 parameters.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
LENGTH
LENGTH(expr)
Function Description: Length in bytes.
Return Result Type: BIGINT.
Applicable Data Types: VARCHAR, NCHAR, VARBINARY.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
LOWER
LOWER(expr)
Function Description: Converts the string argument value to all lowercase letters.
Return Result Type: Same as the original type of the input field.
Applicable Data Types: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
LTRIM
LTRIM(expr)
Function Description: Returns the string after removing left-side spaces.
Return Result Type: Same as the original type of the input field.
Applicable Data Types: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
RTRIM
RTRIM(expr)
Function Description: Returns the string after removing right-side spaces.
Return Result Type: Same as the original type of the input field.
Applicable Data Types: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
TRIM
TRIM([{LEADING | TRAILING | BOTH} [remstr] FROM] expr)
TRIM([remstr FROM] expr)
Function Description: Returns the string expr with all prefixes or suffixes of remstr removed.
Version: ver-3.3.3.0
Return Result Type: Same as the original type of the input field expr.
Applicable Data Types:
- remstr: VARCHAR, NCHAR.
- expr: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- The first optional variable [LEADING | BOTH | TRAILING] specifies which side of the string to trim:
- LEADING removes specified characters from the beginning of the string.
- TRAILING removes specified characters from the end of the string.
- BOTH (default) removes specified characters from both the beginning and the end of the string.
- The second optional variable [remstr] specifies the string to be trimmed:
- If remstr is not specified, spaces are trimmed by default.
- remstr can specify multiple characters, such as trim('ab' from 'abacd'), where 'ab' is treated as a whole to be trimmed, resulting in the trimmed result 'acd'.
- If expr is NULL, returns NULL.
- This function is multibyte safe.
Examples:
taos> select trim(' a ');
trim(' a ') |
=============================
a |
taos> select trim(leading from ' a ');
trim(leading from ' a ') |
==========================================
a |
taos> select trim(leading 'b' from 'bbbbbbbba ');
trim(leading 'b' from 'bbbbbbbba ') |
==============================================
a |
taos> select trim(both 'b' from 'bbbbbabbbbbb');
trim(both 'b' from 'bbbbbabbbbbb') |
=====================================
a |
SUBSTRING/SUBSTR
SUBSTRING/SUBSTR(expr, pos [, len])
SUBSTRING/SUBSTR(expr FROM pos [FOR len])
Function Description: Returns a substring of string expr
starting at position pos
. If len
is specified, it returns the substring starting at position pos
with length len
.
Return Result Type: Same as the original type of the input field expr
.
Applicable Data Types:
expr
: VARCHAR, NCHAR.pos
: Integer type.len
: Integer type.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
pos
is positive, the result is the substring ofexpr
starting from the left to the right from positionpos
. - If
pos
is negative, the result is the substring ofexpr
starting from the right to the left from positionpos
. - If any argument is NULL, returns NULL.
- This function is multi-byte safe.
- If
len
is less than 1, returns an empty string. pos
is 1-based; ifpos
is 0, returns an empty string.- If
pos
+len
exceedslen(expr)
, returns the substring frompos
to the end of the string, equivalent to executingsubstring(expr, pos)
. - Function
SUBSTRING
is equal toSUBSTR
, supported from ver-3.3.3.0. - Syntax
SUBSTRING/SUBSTR(expr FROM pos [FOR len])
is supported from ver-3.3.3.0.
Examples:
taos> select substring('tdengine', 0);
substring('tdengine', 0) |
===========================
|
taos> select substring('tdengine', 3);
substring('tdengine', 3) |
===========================
engine |
taos> select substring('tdengine', 3,3);
substring('tdengine', 3,3) |
=============================
eng |
taos> select substring('tdengine', -3,3);
substring('tdengine', -3,3) |
==============================
ine |
taos> select substring('tdengine', -3,-3);
substring('tdengine', -3,-3) |
===============================
|
SUBSTRING_INDEX
SUBSTRING_INDEX(expr, delim, count)
Function Description: Returns a substring of expr
cut at the position where the delimiter appears the specified number of times.
Version: ver-3.3.3.0
Return Result Type: Same as the original type of the input field expr
.
Applicable Data Types:
expr
: VARCHAR, NCHAR.delim
: VARCHAR, NCHAR.count
: INTEGER.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
count
is positive, the result is the substring ofexpr
from the left to the right up to the position wheredelim
appears for thecount
time. - If
count
is negative, the result is the substring ofexpr
from the right to the left up to the position wheredelim
appears for the absolute value ofcount
. - If any argument is NULL, returns NULL.
- This function is multi-byte safe.
Examples:
taos> select substring_index('www.tdengine.com','.',2);
substring_index('www.tdengine.com','.',2) |
============================================
www.tdengine |
taos> select substring_index('www.tdengine.com','.',-2);
substring_index('www.tdengine.com','.',-2) |
=============================================
tdengine.com |
UPPER
UPPER(expr)
Function Description: Converts the string argument value to all uppercase letters.
Return Result Type: Same as the original type of the input field.
Applicable Data Types: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
CHAR
CHAR(expr1 [, expr2] [, expr3] ...)
Function Description: Treats the input parameters as integers and returns the characters corresponding to these integers in ASCII encoding.
Version: ver-3.3.3.0
Return Result Type: VARCHAR.
Applicable Data Types: Integer types, VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- Values exceeding 255 will be converted into multi-byte results, such as
CHAR(256)
equivalent toCHAR(1,0)
,CHAR(256 * 256)
equivalent toCHAR(1,0,0)
. - NULL values in input parameters will be skipped.
- If the input parameters are of string type, they will be converted to numeric type for processing.
- If the character corresponding to the input parameter is a non-printable character, the return value will still contain the character corresponding to that parameter, but it may not be displayed.
- This function can have at most 2^31 - 1 input parameters.
Examples:
taos> select char(77);
char(77) |
===========
M |
taos> select char(77,77);
char(77,77) |
==============
MM |
taos> select char(77 * 256 + 77);
char(77 * 256 + 77) |
======================
MM |
taos> select char(77,NULL,77);
char(77,null,77) |
===================
MM |
ASCII
ASCII(expr)
Function Description: Returns the ASCII code of the first character of the string.
Version: ver-3.3.3.0
Return Result Data Type: BIGINT.
Applicable Data Types: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
expr
is NULL, return NULL. - If the first character of
expr
is a multi-byte character, only the ASCII code of the first byte of that character will be returned.
Examples:
taos> select ascii('testascii');
ascii('testascii') |
=====================
116 |
POSITION
POSITION(expr1 IN expr2)
Function Description: Calculates the position of string expr1
in string expr2
.
Version: ver-3.3.3.0
Return Result Type: BIGINT.
Applicable Data Types:
expr1
: VARCHAR, NCHAR.expr2
: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
expr1
orexpr2
is NULL, return NULL. - If
expr1
does not exist inexpr2
, return 0. - If
expr1
is an empty string, it is considered to always successfully match inexpr2
, returning 1. - The returned position is 1-based.
- This function is multi-byte safe.
Examples:
taos> select position('a' in 'cba');
position('a' in 'cba') |
=========================
3 |
taos> select position('' in 'cba');
position('' in 'cba') |
========================
1 |
taos> select position('d' in 'cba');
position('d' in 'cba') |
=========================
0 |
REPLACE
REPLACE(expr, from_str, to_str)
Function Description: Replaces all occurrences of from_str
in the string with to_str
.
Version: ver-3.3.3.0
Return Type: Same as the original type of the input field expr
.
Applicable Data Types:
expr
: VARCHAR, NCHAR.from_str
: VARCHAR, NCHAR.to_str
: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes:
- This function is case-sensitive.
- If any argument is NULL, returns NULL.
- This function is multibyte safe.
Example:
taos> select replace('aabbccAABBCC', 'AA', 'DD');
replace('aabbccAABBCC', 'AA', 'DD') |
======================================
aabbccDDBBCC |
REPEAT
REPEAT(expr, count)
Function Description: Returns a string that repeats the string expr
a specified number of times.
Version: ver-3.3.3.0
Return Type: Same as the original type of the input field expr
.
Applicable Data Types:
expr
: VARCHAR, NCHAR.count
: INTEGER.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes:
- If
count < 1
, returns an empty string. - If
expr
orcount
is NULL, returns NULL.
Example:
taos> select repeat('abc',5);
repeat('abc',5) |
============================
abcabcabcabcabc |
taos> select repeat('abc',-1);
repeat('abc',-1) |
===================
|
Conversion Functions
Conversion functions convert values from one data type to another.
CAST
CAST(expr AS type_name)
Function Description: Data type conversion function, returns the result of converting expr
to the type specified by type_name
.
Return Type: The type specified in CAST (type_name
).
Applicable Data Types: The type of input parameter expr
can be any type except JSON and VARBINARY. If type_name
is VARBINARY, then expr
must be of VARCHAR type.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes:
- Unsupported type conversions will result in an error.
- For supported types, if some values cannot be correctly converted, the output of the conversion function will prevail. Current possible scenarios include:
- Invalid character situations when converting string types to numeric types, e.g., "a" might convert to 0, but will not throw an error.
- When converting to numeric types, if the value exceeds the range that
type_name
can represent, it will overflow, but will not throw an error. - When converting to string types, if the converted length exceeds the length specified in
type_name
, it will be truncated, but will not throw an error.
TO_ISO8601
TO_ISO8601(expr [, timezone])
Function Description: Converts a timestamp into the ISO8601 standard date and time format, with additional timezone information. The timezone
parameter allows users to specify any timezone information for the output. If the timezone
parameter is omitted, the output will include the current client system's timezone information.
Return Data Type: VARCHAR type.
Applicable Data Types: INTEGER, TIMESTAMP.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes:
- The
timezone
parameter accepts timezone formats: [z/Z, +/-hhmm, +/-hh, +/-hh:mm]. For example, TO_ISO8601(1, "+00:00"). - The precision of the input timestamp is determined by the precision of the table queried, if no table is specified, the precision is milliseconds.
TO_JSON
TO_JSON(str_literal)
Function Description: Converts a string literal to JSON type.
Return Data Type: JSON.
Applicable Data Types: JSON strings, in the form '{ "literal" : literal }'. '{}' represents a null value. Keys must be string literals, and values can be numeric literals, string literals, boolean literals, or null literals. Escape characters are not supported in str_literal.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
TO_UNIXTIMESTAMP
TO_UNIXTIMESTAMP(expr [, return_timestamp])
return_timestamp: {
0
| 1
}
Function Description: Converts a datetime format string into a timestamp.
Return Data Type: BIGINT, TIMESTAMP.
Applicable Fields: VARCHAR, NCHAR.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Notes:
- The input datetime string must conform to the ISO8601/RFC3339 standards, and formats that cannot be converted will return NULL.
- The precision of the returned timestamp is consistent with the time precision setting of the current DATABASE.
- return_timestamp specifies whether the function's return value is of TIMESTAMP type; setting it to 1 returns TIMESTAMP type, setting it to 0 returns BIGINT type. If not specified, it defaults to BIGINT type.
TO_CHAR
TO_CHAR(ts, format_str_literal)
Function Description: Converts a timestamp type to a string according to the specified format
Version: ver-3.2.2.0
Return Data Type: VARCHAR
Applicable Fields: TIMESTAMP
Nested Subquery Support: Applicable to both inner and outer queries
Applicable to: Tables and supertables
Supported Formats
Format | Description | Example |
---|---|---|
AM,am,PM,pm | AM/PM without dots | 07:00:00am |
A.M.,a.m.,P.M.,p.m. | AM/PM with dots | 07:00:00a.m. |
YYYY,yyyy | Year, 4 or more digits | 2023-10-10 |
YYY,yyy | Year, last 3 digits | 023-10-10 |
YY,yy | Year, last 2 digits | 23-10-10 |
Y,y | Year, last digit | 3-10-10 |
MONTH | Month, uppercase | 2023-JANUARY-01 |
Month | Month, first letter uppercase | 2023-January-01 |
month | Month, lowercase | 2023-january-01 |
MON | Month, abbreviation, uppercase (three characters) | JAN, SEP |
Mon | Month, abbreviation, first letter uppercase | Jan, Sep |
mon | Month, abbreviation, lowercase | jan, sep |
MM,mm | Month, numeric 01-12 | 2023-01-01 |
DD,dd | Day of the month, 01-31 | |
DAY | Day of the week, uppercase | MONDAY |
Day | Day of the week, first letter uppercase | Monday |
day | Day of the week, lowercase | monday |
DY | Day of the week, abbreviation, uppercase | MON |
Dy | Day of the week, abbreviation, first letter uppercase | Mon |
dy | Day of the week, abbreviation, lowercase | mon |
DDD | Day of the year, 001-366 | |
D,d | Day of the week, numeric, 1-7, Sunday(1) to Saturday(7) | |
HH24,hh24 | Hour, 00-23 | 2023-01-30 23:59:59 |
hh12,HH12, hh, HH | Hour, 01-12 | 2023-01-30 12:59:59PM |
MI,mi | Minute, 00-59 | |
SS,ss | Second, 00-59 | |
MS,ms | Millisecond, 000-999 | |
US,us | Microsecond, 000000-999999 | |
NS,ns | Nanosecond, 000000000-999999999 | |
TZH,tzh | Time zone hours | 2023-01-30 11:59:59PM +08 |
Usage Instructions:
- The output format for
Month
,Day
, etc., is left-aligned with spaces added to the right, such as2023-OCTOBER -01
,2023-SEPTEMBER-01
. September has the longest number of letters among the months, so there is no space for September. Weeks are similar. - When using
ms
,us
,ns
, the output of the above three formats only differs in precision, for example, if ts is1697182085123
, the output forms
is123
, forus
is123000
, and forns
is123000000
. - Content in the time format that does not match the rules will be output directly. If you want to specify parts of the format string that can match rules not to be converted, you can use double quotes, like
to_char(ts, 'yyyy-mm-dd "is formated by yyyy-mm-dd"')
. If you want to output double quotes, then add a backslash before the double quotes, liketo_char(ts, '\"yyyy-mm-dd\"')
will output"2023-10-10"
. - Formats that output numbers, such as
YYYY
,DD
, uppercase and lowercase have the same meaning, i.e.,yyyy
andYYYY
are interchangeable. - It is recommended to include timezone information in the time format; if not included, the default output timezone is the timezone configured by the server or client.
- The precision of the input timestamp is determined by the precision of the table queried; if no table is specified, then the precision is milliseconds.
TO_TIMESTAMP
TO_TIMESTAMP(ts_str_literal, format_str_literal)
Function Description: Converts a string to a timestamp according to the specified format.
Version: ver-3.2.2.0
Return Result Data Type: TIMESTAMP
Applicable Fields: VARCHAR
Nested Subquery Support: Applicable to inner and outer queries
Applicable to: Tables and supertables
Supported Formats: Same as to_char
Usage Instructions:
- If
ms
,us
,ns
are specified at the same time, then the resulting timestamp includes the sum of these three fields. For example,to_timestamp('2023-10-10 10:10:10.123.000456.000000789', 'yyyy-mm-dd hh:mi:ss.ms.us.ns')
outputs the timestamp corresponding to2023-10-10 10:10:10.123456789
. MONTH
,MON
,DAY
,DY
and other formats that output numbers have the same meaning in uppercase and lowercase, such asto_timestamp('2023-JANUARY-01', 'YYYY-month-dd')
,month
can be replaced withMONTH
orMonth
.- If the same field is specified multiple times, the earlier specification will be overridden. For example,
to_timestamp('2023-22-10-10', 'yyyy-yy-MM-dd')
, the output year is2022
. - To avoid using an unintended timezone during conversion, it is recommended to carry timezone information in the time, for example, '2023-10-10 10:10:10+08'; if no timezone is specified, the default timezone is the one specified by the server or client.
- If a complete time is not specified, then the default time value is
1970-01-01 00:00:00
in the specified or default timezone, and the unspecified parts use the corresponding parts of this default value. Formats that only specify the year and day without specifying the month and day, like 'yyyy-mm-DDD', are not supported, but 'yyyy-mm-DD' is supported. - If the format string contains
AM
,PM
, etc., then the hour must be in 12-hour format, ranging from 01-12. to_timestamp
conversion has a certain tolerance mechanism; even when the format string and timestamp string do not completely correspond, conversion is sometimes possible, like:to_timestamp('200101/2', 'yyyyMM1/dd')
, the extra 1 in the format string will be discarded. Extra whitespace characters (spaces, tabs, etc.) in the format string and timestamp string will also be automatically ignored. For example,to_timestamp(' 23 年 - 1 月 - 01 日 ', 'yy 年-MM月-dd日')
can be successfully converted. Although fields likeMM
require two digits (with a leading zero if only one digit), into_timestamp
, a single digit can also be successfully converted.- The precision of the output timestamp is the same as the precision of the queried table; if no table is specified, then the output precision is milliseconds. For example,
select to_timestamp('2023-08-1 10:10:10.123456789', 'yyyy-mm-dd hh:mi:ss.ns')
will truncate microseconds and nanoseconds. If a nanosecond table is specified, truncation will not occur, likeselect to_timestamp('2023-08-1 10:10:10.123456789', 'yyyy-mm-dd hh:mi:ss.ns') from db_ns.table_ns limit 1
.
Time and Date Functions
Time and date functions operate on timestamp types.
All functions that return the current time, such as NOW, TODAY, and TIMEZONE, are calculated only once in a SQL statement, no matter how many times they appear.
NOW
NOW()
Function Description: Returns the current system time of the client.
Return Result Data Type: TIMESTAMP.
Applicable Fields: When used in WHERE or INSERT statements, it can only be applied to fields of TIMESTAMP type.
Applicable to: Tables and supertables.
Nested Subquery Support: Applicable to both inner and outer queries.
Usage Instructions:
- Supports time addition and subtraction operations, such as NOW() + 1s. Supported time units include: b(nanoseconds), u(microseconds), a(milliseconds), s(seconds), m(minutes), h(hours), d(days), w(weeks).
- The precision of the returned timestamp is consistent with the time precision set in the current DATABASE.
TIMEDIFF
TIMEDIFF(expr1, expr2 [, time_unit])
Function Description: Returns the result of the timestamp expr1
- expr2
, which may be negative, and approximated to the precision specified by the time_unit
.
Return Result Type: BIGINT.
Applicable Data Types:
expr1
: BIGINT, TIMESTAMP types representing timestamps, or VARCHAR, NCHAR types in ISO8601/RFC3339 standard date-time format.expr2
: BIGINT, TIMESTAMP types representing timestamps, or VARCHAR, NCHAR types in ISO8601/RFC3339 standard date-time format.time_unit
: See usage instructions.timediff
return the absolute value of the difference between timestampexpr1
andexpr2
before ver-3.3.3.0.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- Supported time units
time_unit
include: 1b(nanoseconds), 1u(microseconds), 1a(milliseconds), 1s(seconds), 1m(minutes), 1h(hours), 1d(days), 1w(weeks). - If the time unit
time_unit
is not specified, the precision of the returned time difference is consistent with the time precision set in the current DATABASE. - Returns NULL if the input contains strings that do not conform to the date-time format.
- Returns NULL if
expr1
orexpr2
is NULL. - If
time_unit
is NULL, it is equivalent to the time unit not being specified. - The precision of the input timestamp is determined by the precision of the table being queried; if no table is specified, the precision is milliseconds.
Example:
taos> select timediff('2022-01-01 08:00:00', '2022-01-01 08:00:01',1s);
timediff('2022-01-01 08:00:00', '2022-01-01 08:00:01',1s) |
============================================================
-1 |
taos> select timediff('2022-01-01 08:00:01', '2022-01-01 08:00:00',1s);
timediff('2022-01-01 08:00:01', '2022-01-01 08:00:00',1s) |
============================================================
1 |
TIMETRUNCATE
TIMETRUNCATE(expr, time_unit [, use_current_timezone])
use_current_timezone: {
0
| 1
}
Function Description: Truncates the timestamp according to the specified time unit time_unit
.
Return Result Data Type: TIMESTAMP.
Applicable Fields: BIGINT, TIMESTAMP types representing timestamps, or VARCHAR, NCHAR types in ISO8601/RFC3339 standard date-time format.
Applicable to: Tables and supertables.
Usage Instructions:
- Supported time units
time_unit
include: 1b(nanoseconds), 1u(microseconds), 1a(milliseconds), 1s(seconds), 1m(minutes), 1h(hours), 1d(days), 1w(weeks). - The precision of the returned timestamp is consistent with the time precision set in the current DATABASE.
- The precision of the input timestamp is determined by the precision of the table being queried; if no table is specified, the precision is milliseconds.
- Returns NULL if the input contains strings that do not conform to the date-time format.
- When using 1d/1w as the time unit to truncate timestamps, the
use_current_timezone
parameter can be set to specify whether to truncate based on the current timezone. A value of 0 means truncation using the UTC timezone, and a value of 1 means truncation using the current timezone. For example, if the client's configured timezone is UTC+0800, then TIMETRUNCATE('2020-01-01 23:00:00', 1d, 0) returns the East Eight Zone time '2020-01-01 08:00:00'. Using TIMETRUNCATE('2020-01-01 23:00:00', 1d, 1) returns the East Eight Zone time '2020-01-01 00:00:00'. Whenuse_current_timezone
is not specified, the default value is 1. - When truncating the time value to a week (1w), the calculation of timetruncate is based on the Unix timestamp (January 1, 1970, 00:00:00 UTC). Since the Unix timestamp starts on a Thursday, all truncated dates are Thursdays.
TIMEZONE
TIMEZONE()
Function Description: Returns the current timezone information of the client.
Return Data Type: VARCHAR.
Applicable Fields: None
Applicable to: Tables and supertables.
TODAY
TODAY()
Function Description: Returns the system time at midnight of the current day for the client.
Return Data Type: TIMESTAMP.
Applicable Fields: Can only be used with TIMESTAMP type fields when used in WHERE or INSERT statements.
Applicable to: Tables and supertables.
Usage Instructions:
- Supports time addition and subtraction operations, such as TODAY() + 1s. Supported time units include: b(nanoseconds), u(microseconds), a(milliseconds), s(seconds), m(minutes), h(hours), d(days), w(weeks).
- The precision of the returned timestamp is consistent with the time precision set for the current DATABASE.
WEEK
WEEK(expr [, mode])
Function Description: Returns the week number of the input date.
Version: ver-3.3.3.0
Return Result Type: BIGINT.
Applicable Data Types:
expr
: BIGINT, TIMESTAMP type representing a timestamp, or VARCHAR, NCHAR type conforming to ISO8601/RFC3339 date and time standards.mode
: An integer between 0 - 7.
Nested Subquery Support: Applicable to both inner and outer queries.
Applicable to: Tables and supertables.
Usage Instructions:
- If
expr
is NULL, returns NULL. - The precision of the input timestamp is determined by the precision of the table queried; if no table is specified, the precision is milliseconds.
mode
is used to specify whether the week starts on Sunday or Monday, and whether the return value range is 1 - 53 or 0 - 53. The following table details the calculation methods corresponding to different modes:
Mode | First Day of the Week | Return Value Range | Calculation Method for the 1st Week |
---|---|---|---|
0 | Sunday | 0 - 53 | The first week containing Sunday is week 1 |
1 | Monday | 0 - 53 | The first week containing at least four days is week 1 |
2 | Sunday | 1 - 53 | The first week containing Sunday is week 1 |
3 | Monday | 1 - 53 | The first week containing at least four days is week 1 |
4 | Sunday | 0 - 53 | The first week containing at least four days is week 1 |
5 | Monday | 0 - 53 | The first week containing Monday is week 1 |
6 | Sunday | 1 - 53 | The first week containing at least four days is week 1 |
7 | Monday | 1 - 53 | The first week containing Monday is week 1 |
- When the return value range is 0 - 53, dates before the 1st week are considered week 0.
- When the return value range is 1 - 53, dates before the 1st week are considered the last week of the previous year.
- For example, with
2000-01-01
,- In
mode=0
, the return value is0
because the first Sunday of that year is2000-01-02
, making2000-01-02
the start of week 1, thus2000-01-01
is week 0, returning 0. - In
mode=1
, the return value is0
because the week containing2000-01-01
only has two days,2000-01-01 (Saturday)
and2000-01-02 (Sunday)
, making2000-01-03
the start of the first week, thus2000-01-01
is week 0, returning 0. - In
mode=2
, the return value is52
because2000-01-02
starts week 1, and with the return value range being 1-53,2000-01-01
is considered the last week of the previous year, i.e., the 52nd week of 1999, returning 52.
- In
Example:
taos> select week('2000-01-01',0);
week('2000-01-01',0) |
========================
0 |
taos> select week('2000-01-01',1);
week('2000-01-01',1) |
========================
0 |
taos> select week('2000-01-01',2);
week('2000-01-01',2) |
========================
52 |
taos> select week('2000-01-01',3);
week('2000-01-01',3) |
========================
52 |