search
HomeDatabaseOracle19 commonly used Oracle built-in functions

1. nvl(expression1, expression2)

Function function: Return a non-null value from two expressions

Use example: select nvl(father_name, mother_name) parent_name from student where student_id = '12345'

Note:

If the value of expression1 is not empty, the value of expression1 will be taken first;

If the value of expression1 is empty and If the value of expression2 is not empty, the value of expression2 is taken;

If expression1 and expression2 are both empty, the result is NULL

2. decode(field_name, value1, new_value1, value2 , new_value2, default_value)

Function function: similar to if...else... statement block, for a certain field, if its value is value1, it is converted to newValue1, if the value is value2 , it is converted to newValue2, and the default value is displayed in other cases

Use case: select decode(id,'1','A','2','B',id) from A;

Note:

decode(field_name, value1, new_value1, value2, new_value2, default_value) where value1, newValue1, etc. can be an expression

3. row_number(order by field_name )

Function function: Sort the data set according to a certain field and generate a serial number field

Use example: select row_number() over(order by name) no,id,name from a;

4. to_date(source_string, formater_string)

Function: Convert string to date type

Use example: select to_date(' 20190809','yyyyMMdd') from dual;

Note:

'yyyyMMdd','yyyymmdd','yyyy-MM-dd','yyyy-mm-dd' are all acceptable

5. to_char()

Function function: Convert other types to string types

Use case 1: select to_char(sysdate, 'yyyymmdd' ) from dual

Use case 2: select to_char(99, 'fm999.00') from dual

Note:

There are many other date formats in use case 1, Such as yyyy, mm, dd, D, DD, DDD, etc.

In use case 2, fm, 9, 0 have different meanings, as shown in the following table

Character flag Meaning
9 If a number exists, it will display the number, if it does not exist, it will display a space
0 If the number exists, the number will be displayed. If it does not exist, it will display 0, that is, the placeholder
fm Delete the spaces caused by 9

6. wm_concat

function function: line Column transfer, aggregating multi-row query results into a certain column of a row

Use example: select wm_concat(distinct name) from student

Note: higher versions of Oracle may remove

7. listagg() within group(order by field_name) over(partition by field_name)

Function function: same as wm_concat

Use example: select listagg(distinct name) within group(order by name desc) from student

8. concat(expression1, expression2)

Function function: String splicing function

Use case :select concat('left', 'Right') from dual

Note: You can also use || to splice, select 'a'||'b' from dual

9. sys_guid()

Function: Generate and return a globally unique identifier (original value) consisting of 16 bytes and 32 characters

Use example: select sys_guid () from dual;

Note: often used as the primary key of the table

10. over(partition by field_name, order by field_name)

Function: The over function is an analytical function. When used together with the aggregate function, the code can be simplified.

Use example:

select name, job, sal, deptno,
sum(sal) over(partition by deptno) sum_sal,     --统计某组中的总计值
avg(sal) over(partition by deptno) avg_sal,     --统计某组中的平均值
from emp;

Note: According to the previous writing method, advanced grouping statistics are generated to generate temporary tables and associate the original tables. Only then can you get other information, which is no longer needed now

Usually used with aggregate functions such as max(), min(), avg(), sum()

11 . nlssort

Function: Provide special sorting of Simplified Chinese

Use example:

select * from student order by nlssort(name, 'nls_sort = schinese_pinyin_m') --拼音
select * from team order by nlssort(name, 'nls_sort = schinese_stroke_m')    --笔画
select * from team order by nlssort(name, 'nls_sort = schinese_radical_m')   --部首

12. trunc

Function: intercept dates or numbers and return the specified value according to the rules

Use case 1: select trunc(sysdate, 'yyyy') from dual

Use case 2: select trunc(126.56, 0 ) from dual

Note:

There are other rules in use case 1 that represent different meanings

Rules Meanings
mm Return to the first day of the month
yy Return The first day of the year
dd Returns the current year, month and day
yyyy Returns the first day of the year Day
d Returns the first day of the current week

Use Case 2 There are other rules that represent different Meaning

Rule Meaning
0 Default Value, take and round
Positive number a Retain a decimal place after the decimal point, and erase the others. If a is more than the number of decimal places after the decimal point, keep it Original value
Negative number b Remove everything after the decimal point, move the decimal point abs(b) places to the left, and replace the erased numbers with 0. If all the numbers are If erased, 0
is returned.

13. rank() over(partition by field_name order by field_name)

Function function: Let the returned results generate a ranking relationship based on the partition and sorting fields

Use case :select rank() over(partition by birthday order by score), s.* from student s;

Note: The usage of dense_rank() is the same as rank(), the difference is whether the ranking jumps

14. substr(source, start [,length])

Function: intercept string

Use example: select substr('abcde', 2, 3) from dual

Note: oracle string index starts from 1

15. replace(field_name, sub_str, replace_str)

Function function: will specify Replace the string with the specified string

Use example: select replace(name, 'hello', 'world') from student;

Note: It can also be used in the set part of the update statement

16. trim

Function function: remove the blank characters at the left and right ends

Use example: select trim(' dsf ') from dual;

Note: Use ltrim to remove only the left blank characters, and rtrim to remove only the right blank characters

17. sign

Function: Get the sign of the number n, If greater than 0, return 1, if less than 0, return -1, if equal to 0, return 0

Use example: select sign(50),sign(-100),sign(0) from dual;

18. round(number[,decimal])

Function function: round the number n, retaining decimal digits

Use example: select round(123.34),round( 123.51),round(123.56,1),round(123.34,-1) from dual;

19. coalesce(expression1,expression2...)

function Function: Return the first value in the expression that is not empty. If all are empty, return a null value

Use example: select coalesce(null,3 5,4 6) value from dual;

Recommended tutorial: "Oracle Tutorial"

The above is the detailed content of 19 commonly used Oracle built-in functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Oracle Software: From Databases to the CloudOracle Software: From Databases to the CloudApr 15, 2025 am 12:09 AM

The development history of Oracle software from database to cloud computing includes: 1. Originated in 1977, it initially focused on relational database management system (RDBMS), and quickly became the first choice for enterprise-level applications; 2. Expand to middleware, development tools and ERP systems to form a complete set of enterprise solutions; 3. Oracle database supports SQL, providing high performance and scalability, suitable for small to large enterprise systems; 4. The rise of cloud computing services further expands Oracle's product line to meet all aspects of enterprise IT needs.

MySQL vs. Oracle: The Pros and ConsMySQL vs. Oracle: The Pros and ConsApr 14, 2025 am 12:01 AM

MySQL and Oracle selection should be based on cost, performance, complexity and functional requirements: 1. MySQL is suitable for projects with limited budgets, is simple to install, and is suitable for small to medium-sized applications. 2. Oracle is suitable for large enterprises and performs excellently in handling large-scale data and high concurrent requests, but is costly and complex in configuration.

Oracle's Purpose: Business Solutions and Data ManagementOracle's Purpose: Business Solutions and Data ManagementApr 13, 2025 am 12:02 AM

Oracle helps businesses achieve digital transformation and data management through its products and services. 1) Oracle provides a comprehensive product portfolio, including database management systems, ERP and CRM systems, helping enterprises automate and optimize business processes. 2) Oracle's ERP systems such as E-BusinessSuite and FusionApplications realize end-to-end business process automation, improve efficiency and reduce costs, but have high implementation and maintenance costs. 3) OracleDatabase provides high concurrency and high availability data processing, but has high licensing costs. 4) Performance optimization and best practices include the rational use of indexing and partitioning technology, regular database maintenance and compliance with coding specifications.

How to delete oracle library failureHow to delete oracle library failureApr 12, 2025 am 06:21 AM

Steps to delete the failed database after Oracle failed to build a library: Use sys username to connect to the target instance. Use DROP DATABASE to delete the database. Query v$database to confirm that the database has been deleted.

How to create cursors in oracle loopHow to create cursors in oracle loopApr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to export oracle viewHow to export oracle viewApr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

How to stop oracle databaseHow to stop oracle databaseApr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

What to do if the oracle log is fullWhat to do if the oracle log is fullApr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.