Built-in functions of OceanBase Database can be directly used in SQL statements. Each function has expected data types for its input parameters. If the input data type is not the expected one, OceanBase Database will attempt to convert the input value to the expected data type before executing the SQL function.
Functions are similar to operators in that they take some data elements as input parameters and return a result. However, functions differ from operators in the form of their input parameters. Functions can have a variable number of parameters, ranging from one to many.
FunctionName (Parameter, Parameter, ...)
Functions can also have no parameters, similar to pseudocolumns. However, pseudocolumns typically return different values for each row in the result set, while functions without any 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, subject to certain usage limitations. For more information, see Compatibility with Oracle.
This chapter categorizes functions into two main types. Each type of function has specific data types for its 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 analytical 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.
Analytical functions, like aggregate functions, perform aggregation calculations on a set of rows (a group of rows). The difference is that an aggregate function returns one value (one row) per group, while an analytical function 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 analytical functions, you must use the special keyword OVER to specify the window. For more information about window functions, see Analytical functions.
Numeric functions
The input and output of numeric functions are numeric types. Most numeric functions return a value 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 power of the specified value. |
| 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 following a normal distribution. |
| POWER | Returns the specified 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 following 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 data type, but the actual size exceeds the maximum limit of the VARCHAR2 data 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 data type and the length exceeds the maximum limit, OceanBase Database does not return the data and displays an error message.
| 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 to the left 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 to the right 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 numbers
String functions that return numbers return numbers 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 times a regular expression matches 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 parameters: 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 parameters: ADD_MONTHS, CURRENT_DATE, LAST_DAY, and NEXT_DAY.
If you provide a TIMESTAMP type parameter to any of the above functions, OceanBase Database will implicitly convert it to the DATE type before performing the operation and will return a DATE type result.
Before using date and time functions, it is recommended to execute SELECT * FROM NLS_DATABASE_PARAMETERS to view the current NLS format, as shown below:
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 does not match the examples in this section, 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 number of decimal places for seconds to 9 in the result.*/
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 provide 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 the 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 or timestamp. |
| FROM_TZ | Converts a timestamp and time zone to a timestamp with time zone. |
| LAST_DAY | Returns the last day of the month for the 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 day of the week after the 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 the 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 or timestamp 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 the specified format. |
| TZ_OFFSET | Returns the offset for the specified time zone. |
General comparison functions
You can use the functions in this category to quickly find the maximum and minimum values in a set.
| Function | Description |
|---|---|
| GREATEST | Returns the maximum value from the list of parameters. |
| LEAST | Returns the minimum value from the 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 the 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 BINARY_DOUBLE. |
| TO_BINARY_FLOAT | Converts an expression to BINARY_FLOAT. |
| TO_BLOB | Converts an expression to BLOB. |
| TO_CHAR | Converts an expression to a string. |
| TO_CLOB | Converts an expression to 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. |
| NLS_CHARSET_ID | Returns the character set ID corresponding to the specified character set name. |
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-related functions
Null-related functions are used to handle null values in parameters. For most functions, when the input parameter is a null value (NULL), the result is also NULL. In such cases, 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 commission_pct is not NULL, it returns the actual commission value.
| 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. |
| USERENV | Returns the environment information of the current session. |
Hierarchical functions
| Function | Description |
|---|---|
| SYS_CONNECT_BY_PATH | Returns the path of column values from the root to the node, with column values from each row returned by the CONNECT BY condition separated by a specified delimiter. |
JSON functions
JSON functions create, operate on, and search JSON values.
| Function | Description |
|---|---|
| JSON_ARRAY | Creates a JSON array. |
| JSON_ARRAYAGG | Aggregates multiple rows into a JSON array. |
| JSON_EQUAL | Compares whether two JSON values are equal. |
| JSON_EXISTS | Checks whether a specified path exists in a JSON document. |
| JSON_MERGEPATCH | Merges documents using 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 a SELECT statement.
| 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 condition 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 summaries. |
| 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 in that 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 in the group being the result of a window-based calculation. Analytic functions can significantly optimize queries that require self-joins.
| Function | Description |
|---|---|
| ARG_MAX | Returns the argument with the maximum value. |
| ARG_MIN | Returns the argument with the minimum value. |
| AVG | Returns the average of the 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. |
| FIRST_VALUE | Returns the first value in the window. |
| KEEP | Retains values that meet the specified conditions in 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 multiple row values 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 ratio of the 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 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 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 calculate spatial geometry objects | Queries and calculates the properties of spatial geometry objects. |
