Built-in functions of OceanBase Database can be directly used in SQL statements. Each function expects specific data types for its input parameters. If the provided data type does not match the expected type, OceanBase Database attempts to convert the parameter value to the expected data type before executing the SQL function.
Functions are similar to operators in that they take input data elements and return results. However, functions differ from operators in terms of parameter form. Functions can have a variable number of parameters, ranging from one to several.
FunctionName(Parameter, Parameter, ...)
Functions can also have no parameters, similar to pseudo-columns. However, pseudo-columns typically return different values for each row in the result set, while functions without parameters usually return the same value for each row.
Notice
When using functions on LOB columns in SQL statements, OceanBase Database creates temporary LOB columns during SQL and PL processing, with certain usage limitations. For more information, see Comparison with Oracle.
This chapter categorizes functions into two main types, each with specific data types for parameters and return values:
Single-row functions: These include numeric functions, string functions that return strings, string functions that return numbers, date and time functions, general comparison functions, conversion functions, encoding and decoding functions, null value-related functions, environment and identity functions, and hierarchical functions.
Statistical functions: These include aggregate functions and analytic functions.
Single-row functions return a result value for each row in the queried table or view. These functions can be used in SQL statements with clauses such as SELECT, WHERE, START WITH, CONNECT BY, and HAVING.
Analytic functions and aggregate functions both perform aggregation calculations on a set of rows (a group of rows). The difference is that aggregate functions return one value (one row) per group, while analytic functions can return multiple values (multiple rows) per group. A set of rows is also referred to as a window. Aggregate functions are typically used with the GROUP BY clause in SELECT statements. When used, the database divides the rows of the queried table or view into groups and applies the aggregate function to each group, returning one result row per group.
When using analytic functions, the special keyword OVER is required to specify the window. For more information about window functions, see Analytic functions.
Numeric functions
The input and output of numeric functions are numeric types. Most numeric functions return values of the NUMBER type, which can be precise to 38 decimal places. Some advanced algebraic functions such as COS(), COSH(), EXP(), LN(), LOG(), SIN(), SINH(), SQRT(), TAN(), and TANH() return values precise to 36 decimal places. Other algebraic functions such as ACOS(), ASIN(), ATAN(), and ATAN2() return values precise to 30 decimal places.
| Function | Description |
|---|---|
| ABS | Returns the absolute value of a specified numeric expression. |
| ACOS | Returns the angle in radians whose cosine is the specified numeric expression. |
| ASIN | Returns the angle in radians whose sine is the specified numeric expression. |
| ATAN | Returns the angle in radians whose tangent is the specified numeric expression. |
| ATAN2 | Returns the angle in radians whose tangent is the ratio of the two specified numeric expressions. |
| BITAND | Performs a bitwise AND operation on two numeric values. |
| CEIL | Returns the smallest integer greater than or equal to the specified numeric value. |
| COS | Returns the cosine of the specified angle. |
| COSH | Returns the hyperbolic cosine of the specified angle. |
| EXP | Returns e raised to the specified power. |
| FLOOR | Returns the largest integer less than or equal to the specified numeric value. |
| GENERATOR | Generates a random number within the specified range. |
| LN | Returns the natural logarithm of the specified numeric value. |
| LOG | Returns the logarithm of the specified numeric value. |
| MOD | Returns the remainder of the division of two numeric values. |
| NANVL | If the first parameter is NaN, returns the second parameter. |
| NORMAL | Generates a random number that follows a normal distribution. |
| POWER | Returns the specified numeric value raised to the specified power. |
| RANDOM | Generates a random number between 0 and 1. |
| RANDSTR | Generates a random string of the specified length. |
| REMAINDER | Returns the remainder of the division of two numeric values. |
| ROUND | Rounds the numeric value to the specified number of decimal places. |
| SIGN | Returns the sign of the numeric value (-1, 0, or 1). |
| SIN | Returns the sine of the specified angle. |
| SINH | Returns the hyperbolic sine of the specified angle. |
| SQRT | Returns the square root of the specified numeric value. |
| TAN | Returns the tangent of the specified angle. |
| TANH | Returns the hyperbolic tangent of the specified angle. |
| TRUNC | Truncates the numeric value to the specified number of decimal places. |
| UNIFORM | Generates a random number uniformly distributed within the specified range. |
| WIDTH_BUCKET | Distributes the numeric value into the specified number of buckets. |
| ZIPF | Generates a random number that follows a Zipf distribution. |
String functions that return strings
The maximum length of the return value of a string function depends on the data type. For example, if the return value of a string function is of the VARCHAR2 type, but the actual size of the return value exceeds the maximum limit of the VARCHAR2 type, OceanBase Database truncates the result and returns it. However, no prompt is displayed on the client.
Notice
If the return value is of the CLOB type, OceanBase Database does not return the data and displays an error message when the length of the return value exceeds the maximum limit.
| Function | Description |
|---|---|
| CHR | Converts an ASCII value to a character. |
| CONCAT | Concatenates two strings. |
| INITCAP | Capitalizes the first letter of each word in a string. |
| LOWER | Converts a string to lowercase. |
| LPAD | Pads the specified character on the left side of a string. |
| LTRIM | Removes leading spaces or the specified character from a string. |
| NLS_LOWER | Converts a string to lowercase using the specified language environment. |
| NLS_UPPER | Converts a string to uppercase using the specified language environment. |
| NLSSORT | Returns the sort key of a string. |
| REGEXP_REPLACE | Replaces characters in a string using a regular expression. |
| REGEXP_SUBSTR | Extracts a substring from a string using a regular expression. |
| REPLACE | Replaces a substring in a string. |
| RPAD | Pads the specified character on the right side of a string. |
| RTRIM | Removes trailing spaces or the specified character from a string. |
| SUBSTR | Extracts a substring from a string. |
| TRIM | Removes leading and trailing spaces or the specified character from a string. |
| TRANSLATE | Replaces characters in a string with other characters. |
| UPPER | Converts a string to uppercase. |
String functions that return numeric values
String functions that return numeric values return numeric values based on specified conditions, such as ASCII values and the positions of characters (strings).
| Function | Description |
|---|---|
| ASCII | Returns the ASCII value of the first character in a string. |
| INSTR | Returns the position of a substring in a string. |
| LENGTH | Returns the length of a string. |
| REGEXP_COUNT | Returns the number of matches for a regular expression in a string. |
| REGEXP_INSTR | Returns the position where a regular expression matches. |
Date and time functions
The date and time functions support three types of input data: date and time (DATE), timestamp (TIMESTAMP, TIMESTAMP WITH TIME ZONE, and TIMESTAMP WITH LOCAL TIME ZONE), and interval (INTERVAL DAY TO SECOND and INTERVAL YEAR TO MONTH).
The following functions only support the DATE data type as input: ADD_MONTHS, CURRENT_DATE, LAST_DAY, and NEXT_DAY.
If you provide a TIMESTAMP type as input to the above functions, OceanBase Database will implicitly convert it to the DATE type before passing it to the function for calculation and will return a DATE type result.
Before using date and time functions, we recommend that you execute SELECT * FROM NLS_DATABASE_PARAMETERS to view the current NLS format, as shown in the following example:
obclient> SELECT * FROM NLS_DATABASE_PARAMETERS;
+-------------------------+------------------------------+
| PARAMETER | VALUE |
+-------------------------+------------------------------+
| NLS_DATE_FORMAT | DD-MON-RR |
| NLS_TIMESTAMP_FORMAT | YYYY-MM-DD HH24:MI:SS |
| NLS_TIMESTAMP_TZ_FORMAT | DD-MON-RR HH.MI.SSXFF AM TZR |
| NLS_TERRITORY | AMERICA |
| NLS_SORT | BINARY |
| NLS_COMP | BINARY |
| NLS_CHARACTERSET | AL32UTF8 |
| NLS_NCHAR_CHARACTERSET | AL16UTF16 |
| NLS_DATE_LANGUAGE | AMERICAN |
| NLS_LENGTH_SEMANTICS | BYTE |
| NLS_NCHAR_CONV_EXCP | FALSE |
| NLS_CALENDAR | GREGORIAN |
| NLS_NUMERIC_CHARACTERS | ., |
+-------------------------+------------------------------+
13 rows in set
If the display format in the example is different from the one in the text, you can execute the following commands to change the format:
obclient>ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Query OK, 0 rows affected
obclient>ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF';
Query OK, 0 rows affected
/*Set the decimal places of seconds to 9 in the return value.*/
obclient> ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF TZR TZD';
Query OK, 0 rows affected
Notice
MONTHS_BETWEENreturns a single number.ROUNDandTRUNCcannot perform implicit conversion. You must pass aDATEtype value. Otherwise, an error will be returned.
The remaining functions support all three parameter data types and return values of the same data type as the input parameters.
| Function | Description |
|---|---|
| ADD_MONTHS | Adds a specified number of months to a date. |
| CURRENT_DATE | Returns the current date. |
| CURRENT_TIMESTAMP | Returns the current date and time. |
| DBTIMEZONE | Returns the database time zone. |
| EXTRACT | Extracts a specified part from a date and time. |
| FROM_TZ | Converts a timestamp and time zone to a timestamp with time zone. |
| LAST_DAY | Returns the last day of the month for a specified date. |
| LOCALTIMESTAMP | Returns the current local timestamp. |
| MONTHS_BETWEEN | Returns the number of months between two dates. |
| NEXT_DAY | Returns the date of the next specified weekday after a specified date. |
| NUMTODSINTERVAL | Converts a number to an interval from day to second. |
| NUMTOYMINTERVAL | Converts a number to an interval from year to month. |
| ROUND | Rounds a date to a specified format. |
| SESSIONTIMEZONE | Returns the time zone of the current session. |
| SYSDATE | Returns the current system date and time. |
| SYSTIMESTAMP | Returns the current system timestamp. |
| SYS_EXTRACT_UTC | Extracts the UTC time from a timestamp with time zone. |
| TO_CHAR | Converts a date and time to a string. |
| TO_DSINTERVAL | Converts a string to an interval from day to second. |
| TO_TIMESTAMP | Converts a string to a timestamp. |
| TO_TIMESTAMP_TZ | Converts a string to a timestamp with time zone. |
| TO_YMINTERVAL | Converts a string to an interval from year to month. |
| TRUNC | Truncates a date to a specified format. |
| TZ_OFFSET | Returns the offset of a specified time zone. |
General comparison functions
You can use the functions in this category to quickly find the maximum and minimum values in a collection.
| Function | Description |
|---|---|
| GREATEST | Returns the maximum value from a list of parameters. |
| LEAST | Returns the minimum value from a list of parameters. |
Conversion functions
You can use the functions in this category to convert data from one data type to another.
| Function | Description |
|---|---|
| ASCIISTR | Converts a string to an ASCII string. |
| CAST | Converts an expression to a specified data type. |
| CHARTOROWID | Converts a string to a ROWID. |
| CONVERT | Converts a string from one character set to another. |
| HEXTORAW | Converts a hexadecimal string to RAW data. |
| RAWTOHEX | Converts RAW data to a hexadecimal string. |
| ROWIDTOCHAR | Converts a ROWID to a string. |
| ROWIDTONCHAR | Converts a ROWID to an NCHAR string. |
| SCN_TO_TIMESTAMP | Converts an SCN to a timestamp. |
| TIMESTAMP_TO_SCN | Converts a timestamp to an SCN. |
| TO_BINARY_DOUBLE | Converts an expression to a BINARY_DOUBLE. |
| TO_BINARY_FLOAT | Converts an expression to a BINARY_FLOAT. |
| TO_BLOB | Converts an expression to a BLOB. |
| TO_CHAR | Converts an expression to a string. |
| TO_CLOB | Converts an expression to a CLOB. |
| TO_DATE | Converts a string to a date. |
| TO_MULTI_BYTE | Converts a single-byte character to a multibyte character. |
| TO_NCHAR | Converts an expression to an NCHAR string. |
| TO_NUMBER | Converts an expression to a number. |
| TO_SINGLE_BYTE | Converts a multibyte character to a single-byte character. |
| TO_YMINTERVAL | Converts a string to an interval from year to month. |
| TO_DSINTERVAL | Converts a string to an interval from day to second. |
| TO_TIMESTAMP | Converts a string to a timestamp. |
| TO_TIMESTAMP_TZ | Converts a string to a timestamp with time zone. |
| UNISTR | Converts a string to a Unicode string. |
Encoding and decoding functions
You can use the functions in this category to encode and decode data in OceanBase Database.
| Function | Description |
|---|---|
| DECODE | A conditional expression similar to a CASE statement. |
| DUMP | Returns the internal representation of an expression. |
| ORA_HASH | Calculates the hash value of an expression. |
| VSIZE | Returns the number of bytes in the internal representation of an expression. |
Null value-related functions
Null value-related functions are used to process null values in parameters. For most functions, if the input parameter is a null value (NULL), the output is also NULL. In this case, you can use the NVL function to return a non-null value. For example, if the commission_pct column in a table that records commissions is a null value (NULL), the expression NVL(commission_pct, 0) returns 0. If the commission_pct value is not NULL, the actual commission value is returned.
| Function | Description |
|---|---|
| COALESCE | Returns the first non-null value. |
| LNNVL | Checks whether a condition is null or false. |
| NANVL | If the first parameter is NaN, returns the second parameter. |
| NULLIF | If the two parameters are equal, returns null. |
| NVL | If the first parameter is null, returns the second parameter. |
| NVL2 | If the first parameter is not null, returns the second parameter; otherwise, returns the third parameter. |
Environment-related functions
Environment-related functions provide session or tenant instance-related environment information.
| Function | Description |
|---|---|
| OB_VERSION | Returns the OceanBase Database version. |
| SYS_CONTEXT | Returns the attribute value of the specified context. |
| SYS_GUID | Generates a globally unique identifier. |
| UID | Returns the user ID of the current user. |
| USER | Returns the username of the current user. |
| USERENV | Returns the environment information of the current session. |
Hierarchical functions
| Function | Description |
|---|---|
| SYS_CONNECT_BY_PATH | Returns the path from the root node to the current node. The column values of each row returned by the CONNECT BY condition are separated by the specified delimiter. |
JSON functions
JSON functions create, operate on, and search for JSON values.
| Function | Description |
|---|---|
| JSON_ARRAY | Creates a JSON array. |
| JSON_ARRAYAGG | Aggregates multiple rows into a JSON array. |
| JSON_EQUAL | Compares two JSON values for equality. |
| JSON_EXISTS | Checks whether a specified path exists in a JSON document. |
| JSON_MERGEPATCH | Merges documents using a JSON merge patch. |
| JSON_OBJECT | Creates a JSON object. |
| JSON_OBJECTAGG | Aggregates multiple rows into a JSON object. |
| JSON_QUERY | Queries a value from a JSON document. |
| JSON_TABLE | Converts JSON data into a table format. |
| JSON_VALUE | Extracts a scalar value from a JSON document. |
XML functions
XML functions efficiently query, update, and search XML documents.
| Function | Description |
|---|---|
| DELETEXML | Deletes a node from an XML document. |
| EXISTSNODE | Checks whether a specified node exists in an XML document. |
| EXTRACT | Extracts a node from an XML document. |
| EXTRACTVALUE | Extracts a value from an XML document. |
| INSERTCHILDXML | Inserts a child node into an XML document. |
| UPDATEXML | Updates a node in an XML document. |
| XMLAGG | Aggregates multiple XML values into a single XML document. |
| XMLATTRIBUTES | Creates an XML attribute. |
| XMLCAST | Converts an XML value to a specified data type. |
| XMLCONCAT | Concatenates multiple XML values. |
| XMLELEMENT | Creates an XML element. |
| XMLFOREST | Creates an XML forest. |
| XMLPARSE | Parses a string into an XML document. |
| XMLSEQUENCE | Converts an XML document into a sequence of rows. |
| XMLSERIALIZE | Serializes an XML document into a string. |
| XMLTABLE | Converts XML data into a table format. |
Aggregate functions
Aggregate functions perform calculations on a set of values and return a single value. Aggregate functions ignore null values. Aggregate functions are often used with the GROUP BY clause in SELECT statements.
| Function | Description |
|---|---|
| APPROX_COUNT_DISTINCT | Returns the approximate count of distinct values. |
| AVG | Returns the average of numeric values. |
| CORR | Returns the correlation coefficient of two numeric columns. |
| COUNT | Returns the number of rows or non-null values. |
| COVAR_POP | Returns the population covariance. |
| COVAR_SAMP | Returns the sample covariance. |
| CUME_DIST | Returns the cumulative distribution value. |
| DENSE_RANK | Returns the dense rank. |
| GROUP_ID | Returns the group ID. |
| GROUPING | Returns the grouping information of the GROUP BY clause. |
| GROUPING_ID | Returns the bit vector of the group ID. |
| KEEP | Retains values that meet the specified conditions in the group. |
| LISTAGG | Concatenates multiple row values into a string. |
| MAX | Returns the maximum value. |
| MEDIAN | Returns the median. |
| MIN | Returns the minimum value. |
| PERCENTILE_CONT | Returns the continuous percentile value. |
| PERCENTILE_DISC | Returns the discrete percentile value. |
| PERCENT_RANK | Returns the percentile rank. |
| RANK | Returns the rank. |
| REGR_ | A family of linear regression functions. |
| ROLLUP | Creates group aggregates. |
| STDDEV | Returns the standard deviation. |
| STDDEV_POP | Returns the population standard deviation. |
| STDDEV_SAMP | Returns the sample standard deviation. |
| SUM | Returns the sum of numeric values. |
| VAR_POP | Returns the population variance. |
| VAR_SAMP | Returns the sample variance. |
| VARIANCE | Returns the variance. |
| WMSYS.WM_CONCAT/WM_CONCAT | Concatenates multiple row values into a string. |
Analytic functions
Analytic functions (also known as window functions in some databases) are similar to aggregate functions, as they compute values based on a set of rows. However, while aggregate functions return only one row per group, analytic functions can return multiple rows per group, with each row's value calculated based on the window's logic. Analytic functions can significantly optimize queries that require self-joins.
| Function | Description |
|---|---|
| ARG_MAX | Returns the parameter with the maximum value. |
| ARG_MIN | Returns the parameter with the minimum value. |
| AVG | Returns the average of the values. |
| CORR | Returns the correlation coefficient between two numeric columns. |
| COUNT | Returns the number of rows or non-NULL values. |
| COVAR_POP | Returns the population covariance. |
| COVAR_SAMP | Returns the sample covariance. |
| CUME_DIST | Returns the cumulative distribution value. |
| DENSE_RANK | Returns the dense rank. |
| FIRST_VALUE | Returns the first value in the window. |
| KEEP | Retains values that meet the specified conditions within the group. |
| LAG | Returns the value from the previous row or the previous N rows. |
| LAST_VALUE | Returns the last value in the window. |
| LEAD | Returns the value from the next row or the next N rows. |
| LISTAGG | Concatenates values from multiple rows into a single string. |
| MAX | Returns the maximum value. |
| MEDIAN | Returns the median. |
| MIN | Returns the minimum value. |
| NTH_VALUE | Returns the Nth value in the window. |
| NTILE | Divides the result set into a specified number of groups. |
| PERCENTILE_CONT | Returns the continuous percentile value. |
| PERCENTILE_DISC | Returns the discrete percentile value. |
| PERCENT_RANK | Returns the percentile rank. |
| RANK | Returns the rank. |
| RATIO_TO_REPORT | Returns the proportion of a value within the group. |
| REGR_ | A family of linear regression functions. |
| ROW_NUMBER | Returns the row number. |
| STDDEV | Returns the standard deviation. |
| STDDEV_POP | Returns the population standard deviation. |
| STDDEV_SAMP | Returns the sample standard deviation. |
| SUM | Returns the sum of the values. |
| VAR_POP | Returns the population variance. |
| VAR_SAMP | Returns the sample variance. |
| VARIANCE | Returns the variance. |
| WMSYS.WM_CONCAT/WM_CONCAT | Concatenates values from multiple rows into a single string. |
Information functions
| Function | Description |
|---|---|
| OB_TRANSACTION_ID | Returns the current transaction ID. |
Spatial functions
| Function | Description |
|---|---|
| SDO_GEOMETRY | Creates a spatial geometry object. |
| Query and calculation functions | Queries and calculates the properties of spatial geometry objects. |