Home Database Mysql Tutorial Oracle函数学习笔记分享

Oracle函数学习笔记分享

Jun 07, 2016 pm 04:51 PM
oracle database

1. Oracle单行函数1.1 Oracle字符函数LOWER 使字符串小写;select LOWER(#39;HeLp#39;) from dual ---gt;helpUPPER 使字符串大

1. Oracle单行函数

1.1 Oracle字符函数

LOWER 使字符串小写;
select LOWER('HeLp') from dual --->help

UPPER 使字符串大写;
select UPPER('HeLp') from dual --->HELP

INITCAP 使字符串的第一个字母大写,其它为小写
select INITCAP('hELp') from dual --->Help

LENGTH 返回表达式中的字符串长度
select LENGTH('hELP') from dual ---->4

CONCAT 将值连接到一起(,对于此函数,只能使用2个参数)
select CONCAT('Hello', 'World') from dual --->HelloWorld

SUBSTR 抽取确定长度的字符串
select SUBSTR('HelloWorld',1,5) from dual ---->Hello

INSTR 查找指定字符的数字位置
select INSTR('HelloWorld', 'W') from dual --->6

LPAD 按右对齐填充字符串
select LPAD(salary,10,'*') from dual --->*****24000

RPAD 按左对齐填充字符串
select RPAD(salary, 10, '*') from dual --->24000*****

TRIM(leading|trailing|both,trim_character FROM trim_source) 从字符串中截去头部或者尾部的字符(或者头尾都截掉)(如果trim_character或trim_source是一个字符型文字值,则必须将它包含在单引号之内。)
select TRIM('H' FROM 'HelloWorld') from dual --->elloWorld

REPLACE(text,search_string,replacement_string) 搜索字符串的文本表达式,如果找到,用指定的替代字符串替换它。
select REPLACE('HelloWorld','Hello','hi') from dual --->hiWorld

1.2数字函数

ROUND 将值四舍五入到指定的位数
ROUND(45.926, 2) ---->45.93

TRUNC 将值截断到指定的小数位(不舍入)
TRUNC(45.926, 2) ---->45.92

MOD 返回除法运算的余数
MOD(1600, 300) --->100

1.3 日期函数

在oracle中,日期是以数字的形式存储的,故可以直接对日期类型的数据进行+-*/等运算。

MONTHS_BETWEEN 两个日期之间的月数,结果以月为单位
MONTHS_BETWEEN(date1, date2)

ADD_MONTHS 将日历月份添加到日期中
ADD_MONTHS(date, n) n为月数

NEXT_DAY 指定日期的下一个月
NEXT_DAY(date, 'char') char可以是星期,月数

LAST_DAY 月份的最后一天
LAST_DAY(date)

ROUND 四舍五入日期
ROUND(date[, 'fmt'])

设置日期格式:alter session set nls_date_format='yyyy-mm-dd:hh24:mi:ss'

TRUNC 截断日期
TRUNC(date[, 'fmt'])

1.4 转换函数

TO_CHAR(number|date,'[fmt]',[nlsparams])将数字和日期值转换为格式样式为fmt的varchar2字符串。
nlsparams参数指定返回的月份名称、日期名称以及缩写所用的语言。

TO_NUMBER(char,'[fmt]',[nlsparams])将包含数字的字符串转换为格式样式为fmt指定的格式表示的数字。
nlsparams参数与TO_CHAR()相同,是进行数字转换。

TO_DATE(char,'[fmt]',[nlsparams])将代表日期的字符串按照指定的fmt转换为日期值。如果省略了fmt,则格式为DD-MON-YY。
nslparams参数在此函数中的用途是进行日期转换。

1.5嵌套函数

NVL (expr1, expr2) 将空值转换为实际的值。

NVL2 (expr1, expr2, expr3) 如果expr1为非空,返回expr2。如果expr1为空值,将返回expr3。expr1可以是任何数据类型。

NULLIF (expr1, expr2) 比较2个值,如相等,返回空值,否则返回第一个表达式。

COALESCE (expr1, expr2, ..., exprn) 返回表达式中第一个非空值。

1.6 条件表达式

CASE表达式(符合ANSISQL)
通过执行IF-THEN-ELSE语句的任务,可以简化条件查询
CASE expr WHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END

DECODE函数(Oracle专用语法)
通过执行IF-THEN-ELSE语句的任务,可以简化条件查询
DECODE(col|expression, search1, result1
[, search2, result2,...,]
[, default])


2、分组函数

使用分组函数的准则:
DISTINCT 使函数只考虑非重复值;ALL使之考虑包括重复值在内的所有值。默认值为ALL,因此它不需
要专门指定。
带有expr参数的函数的数据类型可以是CHAR、VARCHAR2、NUMBER或者DATE。
所有分组函数都忽略空值。要用一个值代替空值,可以使用NVL、NVL2或者COALESCE函数。
在使用GROUP BY 字句时,Oracle服务器隐式地按照升序对结构集进行排序。要改写此默认顺序,可以
在ORDER BY 字句中使用DESC。

2.1 AVG([DISTINCT|ALL]n) n的平均值,忽略空值

2.2 COUNT({*|[DISTINCT|ALL]expr}) 行数,其中expr用来判断非空值(使用*计算所有选定行,包括重
复行和带有空值的行)

2.3 MAX([DISTINCT|ALL]expr) expr的最大值,,忽略空值

2.4 MIN([DISTINCT|ALL]expr) expr的最小值,忽略空值
注:可以对任何数据类型使用MIN 和 MAX 函数

2.5 STDDEV([DISTINCT|ALL]x) n的标准偏差,忽略空值

2.6 SUM([DISTINCT|ALL]n) n的总计值,忽略空值

2.7 VARIANCE([DISTINCT|ALL]x) n的方差,忽略空值
例1:
SELECT AVG(salary), MAX(salary),
MIN(salary), SUM(salary)
FROM employees
WHERE job_id LIKE '%REP%';
例2:
SELECT MIN(hire_date), MAX(hire_date)
FROM employees;
例3:
SELECT COUNT(*)
FROM employees
WHERE department_id = 50;

2.8 GROUP BY 字句
语法:GROUP BY group_by_expression group_by_expression--->指定列,这些列的值是对行进行分组的基础。
作用:通过使用GROUP BY字句将表中的行分成更小的组。也可以对多个列使用GROUP BY子句。
例4:
SELECT department_id dept_id, job_id, SUM(salary)
FROM employees
GROUP BY department_id, job_id ;
注意:
A、 SELECT 列表中不是聚合函数的任何列或表达式都必须在GROUP BY子句中。
B、 不能使用 WHERE 子句来限制组;
C、 可以使用HAVING子句来限制组;
D、 不能在 WHERE 子句中使用分组函数。

2.9 嵌套分组函数
分组函数可以嵌套两层。
例5:
SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 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.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to export oracle view How to export oracle view Apr 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.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

What to do if the oracle log is full What to do if the oracle log is full Apr 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.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

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.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

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

See all articles