All Products
Search
Document Center

TSQL functions

Last Updated: Apr 08, 2021

This document mainly introduces TSQL functions.

Aggregate functions

Aggregate functions

Parameter type

Return type

Return value

avg(expression)

SMALLINT,INTEGER,BIGINT,FLOAT,DOUBLE

Same as the data type of the values that are specified by the expression

The average of the values that are specified by the expression

count(*)

N/A

BIGINT

The total number of rows

count(expression)

The values specified by the expression can be of any data type.

BIGINT

The number of values that are not null

count(distinct expression)

The values specified by the expression can be of any data type.

N/A

The number of unique values that are not null

max(expression)

SMALLINT,INTEGER,BIGINT,FLOAT,DOUBLE, VARCHAR

Same as the data type of the values that are specified by the expression

The maximum value that is specified by the expression

min(expression)

SMALLINT,INTEGER,BIGINT,FLOAT,DOUBLE,VARCHAR

Same as the data type of the values that are specified by the expression

The minimum value that is specified by the expression

ts_last(expression, timestamp)

The data types supported by the expression placeholder are DOUBLE, VARCHAR, and BOOLEAN. The data type supported by the timestamp placeholder is TIMESTAMP.

Same as the data type of the values that are specified by the expression

The value at the latest timestamp after the values specified by the expression are sorted by timestamp

ts_first(expression, timestamp)

The data types supported by the expression placeholder are DOUBLE, VARCHAR, and BOOLEAN. The data type supported by the timestamp placeholder is TIMESTAMP.

Same as the data type of the values that are specified by the expression

The value at the earliest timestamp after the values specified by the expression are sorted by timestamp

Mathematical functions

TSQL supports the mathematical functions listed in the following table and the trigonometric functions listed in the next section. The following data types are supported for the input values for most of the mathematical functions and all of the trigonometric functions:

  • INTEGER

  • BIGINT

  • FLOAT

  • DOUBLE

  • SMALLINT

Function

Return type

Description

ABS(x)

Same as the data type of the input value

Returns the absolute value of x.

CBRT(x)

FLOAT8

Returns the cube root of x.

CEIL(x)

Same as the data type of the input value

Returns the smallest integer that is greater than or equal to x.

CEILING(x)

Same as the data type of the input value

Returns the smallest integer that is greater than or equal to x.

DEGREES(x)

FLOAT8

Converts x radians to degrees.

E()

FLOAT8

Returns 2.718281828459045.

EXP(x)

FLOAT8

Returns e raised to the power of x.

FLOOR(x)

Same as the data type of the input value

Returns the largest integer that is smaller than or equal to x.

LOG(x)

FLOAT8

Returns the base e logarithm of x.

LOG(x, y)

FLOAT8

Returns the base x logarithm of y.

LOG10(x)

FLOAT8

Returns the base 10 logarithm of x.

LSHIFT(x, y)

Same as the data type of the input value

Shifts the binary x by y times to the left.

MOD(x, y)

FLOAT8

Returns the remainder of x divided by y.

NEGATIVE(x)

Same as the data type of the input value

Returns x as a negative number.

PI

FLOAT8

Returns Pi.

POW(x, y)

FLOAT8

Returns x raised to the power of y.

RADIANS(x)

FLOAT8

Converts x degrees to radians.

RAND

FLOAT8

Returns a random number that ranges from 0 to 1.

ROUND(x)

Same as the data type of the input value

Rounds to the nearest integer.

RSHIFT(x, y)

Same as the data type of the input value

Shifts the binary x by y times to the right.

SIGN(x)

INT

Returns the sign of x.

SQRT(x)

Same as the data type of the input value

Returns the square root of x.

TRUNC(x, y)

DOUBLE

Truncates x to y decimal places. In this function, y is optional. The default value is 0.

Examples

The math_func_demo table is used in the following examples of mathematical function. The math_func_demo table contains the following data.

select * from math_func_demo; +---------------+-----------------------+ | integer | float | +---------------+-----------------------+ |2010|17.4| |-2002|-1.2| |2001|1.2| |6005|1.2| +---------------+-----------------------+
  • ABS(x)

    SELECT ABS(`integer`) FROM math_func_demo; +------------+ | EXPR$0 | +------------+ | 2010 | | 2002 | | 2001 | | 6005 | +------------+ 4 rows selected (0.357 seconds)
  • CEIL(x)

    SELECT CEIL(`float`) FROM math_func_demo; +------------+ | EXPR$0 | +------------+ | 18.0 | | -1.0 | | 2.0 | | 2.0 | +------------+ 4 rows selected (0.647 seconds)
  • FLOOR(x)

    SELECT FLOOR(`float`) FROM math_func_demo; +------------+ | EXPR$0 | +------------+ | 17.0 | | -2.0 | | 1.0 | | 1.0 | +------------+ 4 rows selected (0.11 seconds)
  • ROUND(x) and ROUND(x, y):The ROUND(x) and Round (x, y) function examples return the nearest integer and an approximate number that is rounded to four decimal places, respectively.

    ``bash 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(x, y), LOG10(x), and LOG(x)The LOG(x, y), LOG10(x), and LOG(x) function examples return the base 2 logarithm of 64, the base 10 logarithm of 100, and the base e logarithm of 7.5, respectively.

    ```bash 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

TSQL supports the following trigonometric functions, which return results of the FLOAT8 type.

  • SIN(x)

    Returns the sine of x.

  • COS(x)

    Returns the cosine of x.

  • TAN(x)

    Returns the tangent of x.

  • ASIN(x)

    Returns the arcsine of x.

  • ACOS(x)

    Returns the arccosine of x.

  • ATAN(x)

    Returns the arctangent of x.

  • SINH(x)

    Returns the hyperbolic sine of x.

  • COSH(x)

    Returns the hyperbolic cosine of x.

  • TANH(x)

    Returns the hyperbolic tangent of x.

Example:

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 function

The following table lists the string functions that are supported by TSQL.

Function

Return type

CONCAT

VARCHAR

INITCAP

VARCHAR

LENGTH

INTEGER

LOWER

VARCHAR

LPAD

VARCHAR

LTRIM

VARCHAR

REGEXP_REPLACE

VARCHAR

RPAD

VARCHAR

RTRIM

VARCHAR

STRPOS

INTEGER

SUBSTR

VARCHAR

TRIM

VARCHAR

UPPER

VARCHAR

Examples

  • CONCAT

    Syntax: CONCAT(string [,string [, ...]])

    The CONCAT function connects strings.

    SELECT CONCAT('Drill',' ',1.0,' ','release') FROM (VALUES(1)); +--------------------+ | EXPR$0 | +--------------------+ |Drill1.0 release | +--------------------+ 1 row selected (0.134 seconds)
  • INITCAP

    Syntax: INITCAP(string)

    The INITCAP function returns a string where the first character is capitalized.

    SELECT INITCAP('china beijing') FROM (VALUES(1)); +---------------------------+ | EXPR$0 | +---------------------------+ |ChinaBeijing| +---------------------------+ 1 row selected (0.106 seconds)
  • LENGTH

    Syntax: LENGTH(string [, encoding])

    The LENGTH function returns the number of characters in a string.

    SELECT LENGTH('Hangzhou') FROM (VALUES(1)); +------------+ | EXPR$0 | +------------+ |8| +------------+ 1 row selected (0.127 seconds)
  • LOWER

    Syntax: LOWER(string)

    The LOWER function converts uppercase characters in a string to lowercase characters.

    SELECT LOWER('China Beijing') FROM (VALUES(1)); +---------------+ | EXPR$0 | +---------------+ | china beijing | +---------------+ 1 row selected (0.103 seconds)
  • LPAD

    Syntax: LPAD(string,length [, fill text])

    The LPAD function left-pads a string to a specified length with the fill text. If the fill text fails to pad the string to the specified length, spaces are added to pad the string to the specified length. If the string length exceeds the specified length, the string is truncated to the specified length.

    SELECT LPAD('hi',5,'xy') FROM (VALUES(1)); +------------------------------+ | EXPR$0 | +------------------------------+ | xyxhi | +------------------------------+ 1 row selected (0.132 seconds)
  • LTRIM

    Syntax: LTRIM(string1, string2)

    The LTRIM function removes all the characters that are contained in the specified set (string2) from the left side of the input string (string1).

    SELECT LTRIM('zzzytest','xyz') FROM (VALUES(1)); +------------+ | EXPR$0 | +------------+ | test | +------------+ 1 row selected (0.131 seconds)
  • REGEXP_REPLACE

    Syntax: REGEXP_REPLACE(source_char, pattern, replacement)

    The REGEXP_REPLACE function replaces the substrings that match the Java regular expression pattern in the string with the specified characters.

    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)
  • RPAD

    Syntax: RPAD (string, length [, fill text])

    The RPAD function right-pads a string to the specified length. If you do not specify the fill text or the fill text fails to pad the string to the specified length, spaces are added to the end of the specified characters. The specified characters follow a specific keyword. If the string length exceeds the specified length, the string is truncated to the specified length.

    SELECT RPAD('hi',5,'xy') FROM (VALUES(1)); +-------------------------+ | EXPR$0 | +-------------------------+ | hixyx | +-------------------------+ 1 row selected (0.107 seconds)
  • RTRIM

    Syntax: RTRIM(string1, string2)

    The RTRIM function removes all the characters that are contained in the specified set (string2) from the right side of the input string (string1).

     SELECT RTRIM('testxxzx','xyz')from(VALUES(1)); +--------------------+ | EXPR$0 | +--------------------+ | tes | +--------------------+ 1 row selected (0.102 seconds)
  • STRPOS

    Syntax: STRPOS(string, substring)

    The STRPOS function returns the position of a substring in a string.

    SELECT STRPOS('high','ig') FROM (VALUES(1)); +------------+ | EXPR$0 | +------------+ |2| +------------+ 1 row selected (0.22 seconds)
  • SUBSTR

    Syntax: SUBSTR(string, x, y)

    The SUBSTR function returns the substring from the x to x+y position in a string. The y argument is optional.

    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

    Syntax: TRIM ([leading | trailing | both] [string1] from string2)

    The TRIM function removes all the characters that are contained in the specified set (string1) from the left side or the right side of a string (string2).

    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)
  • UPPER

    Syntax: UPPER(string)

    The UPPER function converts lowercase characters in a string to uppercase characters.

    SELECT UPPER('china beijing') FROM (VALUES(1)); +---------------+ | EXPR$0 | +---------------+ | CHINA BEIJING | +---------------+ 1 row selected (0.081 seconds)

Timestamp functions

The following table lists the timestamp functions that are supported by TSQL.

Function

Return type

Description

Example

now()

timestamp

Returns the current timestamp.

now()

CURRENT_TIMESTAMP

timestamp

Returns the current timestamp.

CURRENT_TIMESTAMP

CURRENT_DATE

date

Returns the current date.

CURRENT_DATE

CURRENT_TIME

time

Returns current time. The returned value excludes the date information.

EXTRACT(component FROM timestamp/date/time)

integer

Extracts a year, month, day, hour, minute, or second component from a specified timestamp, date, or time.

EXTRACT(day from 'timestamp'

tumble(timestamp, interval)

timestamp

Returns the lower boundary of a tumbling window that contains the specified timestamp.

tumble('timestamp', interval '5' minute)

date_diff(timestamp, interval)

timestamp

Returns a new timestamp after the specified interval is subtracted from a timestamp.

date_diff(timestamp, interval '5' minute)

date_add(timestamp, interval)

timestamp

Returns a new timestamp after the specified interval is added to a timestamp.

date_add(timestamp, interval '5' minute)

The following examples are used to describe how to use the timestamp functions.

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-1620:33:40.0| +------------------------+
SELECT DATE_ADD(TIMESTAMP '2001-02-16 20:38:40', interval '5' minute) FROM (VALUES(1)); +------------------------+ | EXPR$0 | +------------------------+ |2001-02-1620:43:40.0| +------------------------+
SELECT tumble(TIMESTAMP '2001-02-16 20:38:40', interval '5' minute) FROM (VALUES(1)); +------------------------+ | EXPR$0 | +------------------------+ |2001-02-1620:35:00.0| +------------------------+

Data type conversion functions

CAST

The CAST function converts the value of a specified expression from one data type to another data type. A single value can be obtained based on the specified expression.

  • Syntax

    CAST (<expression> AS <data type>)
  • expression

    The expression element specifies one or more values whose data types are to be converted. The specified expression consists of operators and SQL functions.

  • data type

    The data type element specifies the target data type, such as INT or DATE.

    This topic provides the following examples. In these examples, a string is converted to a number, a number is converted to a string, and a number is converted from one data type to another data type, respectively.

    SELECT CAST(456as VARCHAR(3)) FROM (VALUES(1)); +------------+ | EXPR$0 | +------------+ |456| +------------+ 1 row selected (0.08 seconds) SELECT CAST(456as CHAR(3)) FROM (VALUES(1)); +------------+ | EXPR$0 | +------------+ |456| +------------+ 1 row selected (0.093 seconds)

Convert timestamp, date, and time data types

TSQL supports the following date and time formats:

  • 2008-12-15

  • 22:55:55.123...

If the date and time use other formats, use data type conversion functions to perform the following operations:

  • Convert values of the TIMESTAMP, DATE, TIME, INT, FLOAT, or DOUBLE data type to strings of the VARCHAR data type.

  • Convert strings to values of the DATE data type.

  • Convert strings to values of the NUMBER data type.

The following table lists the data type conversion functions that you can use in TSQL queries.

Function

Return Type

TO_CHAR(expression, format)

VARCHAR

TO_DATE(expression, format)

DATE

TO_TIMESTAMP(VARCHAR, format)

TIMESTAMP

TO_TIMESTAMP(DOUBLE)

TIMESTAMP

Format specifier for date /time conversion

The following table lists the Joda-Time formatting symbols that are used for date and time conversion.

Symbol

Meaning

Presentation

Examples

G

era

text

AD

C

century of era (>=0)

number

20

Y

year of era (>=0)

year

1996

x

weekyear

year

1996

w

week of weekyear

number

27

e

day of week

number

2

E

day of week

text

Tuesday; Tue

y

year

year

1996

D

day of year

number

189

M

month of year

month

July; Jul; 07

d

day of month

number

10

a

halfday of day

text

PM

K

hour of halfday (0~11)

number

0

h

clockhour of halfday (1~12) number

12

N/A

H

hour of day (0~23)

number

0

k

clockhour of day (1~24)

number

24

m

minute of hour

number

30

s

second of minute

number

55

S

fraction of second

number

978

z

time zone

text

Pacific Standard Time; PST

Z

time zone offset/id

zone

-0800; -08:00; America/Los_Angeles

'

single quotation mark, escape for text delimiter

literal

N/A

  • TO_CHAR example:

    The TO_CHAR function converts a number, date, time, or timestamp expression to a string.

    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 example:

    The following example converts 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) SELECT EXTRACT(year from mydate)`extracted year` FROM (SELECT TO_DATE('2015-FEB-23','yyyy-MMM-dd') AS mydate FROM (VALUES(1))); +------------+ | myyear | +------------+ |2015| +------------+ 1 row selected (0.128 seconds)

    The following example converts a Unix timestamp to a date.

    SELECT TO_DATE(1427849046000) FROM (VALUES(1)); +------------+ | EXPR$0 | +------------+ |2015-04-01| +------------+ 1 row selected (0.082 seconds)
  • TO_TIME example:

    Converts a string to a time value.

    SELECT to_time(82855000) FROM (VALUES(1));+------+| EXPR$0 |+------+| 23:00:55 |+------+1 row selected (0.086 seconds)
  • TO_TIMESTAMP example:

    Converts a date to a timestamp.

    ```sql 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)

    The following example converts the Unix time 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)

    The following example converts a Coordinated Universal Time (UTC) date to a timestamp that includes a time zone offset in its value.

    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)