Oracle学习笔记4--单行函数
使用函数可以完成一系列的操作。数据库之间的不同,在于对函数的支持上是不一样。 本次笔记学习函数。函数分为单行函数和多行函数,此处重点学习单行函数。 单行函数分类: 字符函数 数值函数 日期函数 转换函数 通用函数 字符函数:又分为大小写控制函数
使用函数可以完成一系列的操作。数据库之间的不同,在于对函数的支持上是不一样。
本次笔记学习函数。函数分为单行函数和多行函数,此处重点学习单行函数。
单行函数分类:
字符函数
数值函数
日期函数
转换函数
通用函数
字符函数:又分为大小写控制函数和字符控制函数
1)大小写空值函数:
LOWER()
UPPER()
INITCAP()
?
1
2
3
4
5
SQL> select lower('SQL COURSE') , upper('sql course') , initcap('SQL course') from dual ;
LOWER('SQLCOURSE') UPPER('SQLCOURSE') INITCAP('SQLCOURSE')
------------------ ------------------ --------------------
sql course SQL COURSE Sql Course
通过上面的查询结果,看出:
Lower() 函数的作用是:将字符转换为小写。
Upper()函数的作用是:将字符转换为大写。
Initcap()函数的作用是:将字符的首字母转换为大写。
例:查询名字是king的雇员信息 ,由于不确定数据库中名字的大小写形式,我们引入lower函数和 upper()函数,来保证查询的有效性。
?
1
2
3
4
5
6
7
8
9
10
11
SQL> select * from emp where lower(ename) = 'king' ;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7839 KING PRESIDENT 1981/11/17 5000.00 10
SQL> select * from emp where ename = upper('king' );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7839 KING PRESIDENT 1981/11/17 5000.00 10
例:将查询结果字符的首字母转换为大写形式,使用initcap 函数。
?
1
SQL> select initcap(ename) from emp where ename = 'KING';
?
1
2
3
INITCAP(ENAME)
--------------
King
字符控制函数:
concat()
substr()
length()
replace()
例:字符串除了使用‘||’连接之外,还可以使用concat() 函数进行连接 。
?
1
2
3
4
5
6
7
8
9
10
11
SQL> select concat('Hello ' , 'world' ) from dual ;
CONCAT('HELLO','WORLD')
-----------------------
Hello world
SQL> select substr('hello' , 1 , 2) ,length('world') , replace('sql' , 'q' , 'X') from dual ;
SUBSTR('HELLO',1,2) LENGTH('WORLD') REPLACE('SQL','Q','X')
------------------- --------------- ----------------------
he 5 sXl
上例中我们可以看到 substr()的作用是截取字符串,在字符串的后面跟两个参数,第一个参数表示的是要截取的
起始位置,第二个参数表示的是截取的个数,length()函数返回的是字符串的长度。replace() 字符串后面跟两
个参数,第一个参数表示要替换的字母,第二个参数表示将第一个字母换为第二个字母。
另外Oracle还支持倒着截取字符串的方式:
?
1
2
3
4
5
6
7
SQL> select ename ,substr(ename ,-3 ) from emp where deptno = 10 ;
ENAME SUBSTR(ENAME,-3)
---------- ----------------
CLARK ARK
KING ING
MILLER LER
数值函数:
round():四舍五入函数
trunc():截断小数位函数
mod() :取余函数
例:
?
1
2
3
4
5
SQL> select round(234.12 , 2) , round(234.324) , round(234.25 , -2) from dual;
ROUND(234.12,2) ROUND(234.324) ROUND(234.25,-2)
--------------- -------------- ----------------
234.12 234 200
后面的参数 2 表示保留小数点后两位,如果不写,默认为零,-2 表示对小数点前面的数进行四舍五入ROUND(234.25,-2) , 4 舍去,3 舍去得到200。
例:
?
1
2
3
4
5
SQL> select trunc(234.12 , 2) , trunc(234.324) , trunc(248.25 , -2) from dual;
TRUNC(234.12,2) TRUNC(234.324) TRUNC(248.25,-2)
--------------- -------------- ----------------

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

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.

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

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

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.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.
