Oracle is one of the world's largest relational database management systems. It provides a wealth of functions to process data. These functions are widely used in SQL and are very useful and can help you better process and manipulate data. In this article, we will learn the commonly used functions, syntax and usage in Oracle.
Oracle function types
In Oracle, functions can be divided into two types: single-row functions and multi-row functions.
Single-row functions
Single-row functions (Scalar Functions) calculate a single value for each row and return a single-row result. They can be used in SELECT statements. The following are common single-line functions:
SELECT UPPER('hello world') FROM dual;
-- Output: HELLO WORLD
SELECT ROUND(3.14159, 3) FROM dual;
-- Output: 3.142
SELECT ADD_MONTHS(SYSDATE, 1) FROM dual;
-- Output: 2022-10-27 11:08:17
Multi-line function
Multiple row functions (Group Functions) calculate and return a single value common to a group of rows. Multi-row functions are often used in the GROUP BY clause of a SELECT statement. The following are common multi-row functions:
SELECT AVG(salary) FROM employees;
SELECT COUNT(*) FROM employees;
SELECT MAX(salary) FROM employees;
SELECT MIN(salary) FROM employees;
SELECT SUM(salary) FROM employees;
Oracle function usage
function The syntax is as follows:
function_name(argument1, argument2, ...)
Where, function_name is the name of the function, argument1, argument2, ... are the parameters of the function. Different functions can have different parameters. For specific usage, please refer to Oracle official documentation.
Let us look at the usage of several common functions:
The UPPER function will input all the characters in the string Convert to uppercase.
For example:
SELECT UPPER('hello world') FROM dual;
-- Output: HELLO WORLD
SUBSTR function can be used to intercept substrings in a string. The syntax is as follows:
SUBSTR(source_string, start_position, length)
Among them, source_string is the source string to be intercepted, start_position is the position to start intercepting, length is the length to be intercepted (optional) parameter, if not specified, it will be truncated to the end of the source string).
For example:
SELECT SUBSTR('Hello, World', 1, 5) FROM dual;
-- Output: Hello
The ROUND function rounds a number to a specified number of decimal places.
For example:
SELECT ROUND(3.14159, 3) FROM dual;
-- Output: 3.142
The SYSDATE function is used to retrieve the current date and time. For example:
SELECT SYSDATE FROM dual;
-- Output: 2021-09-27 11:08:17
The COUNT function returns the number of rows in the specified column.
For example:
SELECT COUNT(*) FROM employees;
-- Output: 107
The above are only a small part of the use of Oracle functions. Oracle has rich Function library, each function has its own unique purpose. Understanding the syntax and usage of Oracle functions will allow you to process data more efficiently.
The above is the detailed content of oracle function usage. For more information, please follow other related articles on the PHP Chinese website!