Oracle中的关键字用法

本文深入讲解了SQL中的各种实用函数,包括字符串处理如Lower、Upper、Initcap、Concat、Substr、Trim等,数学运算如Round、Trunc、Mod,日期操作如Months_between、Add_months,以及数据类型转换如to_date、to_char、to_number。此外,还介绍了NVL、NVL2、NULLIF、Coalesce等数据处理函数,并通过案例展示了Case表达式的使用。

 

Distinct 去重复

Lower 将字符全部改为小写

例如:Lower('HappY') 结果为:happy

Upper 将字符全部改为大写

例如:Upper('HappY') 结果为:HAPPY

Initcap 字符首字母大写

例如:Initcap('HappY') 结果为:Happy

Concat 连接两个字符串

Substr 字符截取函数,从…截取到…:例如:Substr ('HelloWorld',1,5)结果:Hello

Length 字符长度。例如:Length ('HelloWorld'),结果:10

Instr 指定字符所在的位置

举例说明:Lower、Upper、Initcap、concat、substr、Length、instr函数

select Lower('HappY'),

Upper('HappY'),

Initcap('HappY') as Initcap,

concat('Happy','Birthday') as concat,

Substr('HelloWorld',1,5) as Substr,

Length ('HelloWorld') as Length,

instr('Happy birthday', 'b') as instr

from dual;

Instr的其他用法:

 

Lpad | Rpad 左对齐/右对齐  

例如:LPAD(salary,10,'*'),结果:*****11111   RPAD(salary, 10, '*')。结果: 11111*****

Trim去除指定位置的字符(字符串首部和尾部) 没有特殊规定就是去空格

例如:Trim ('H' FROM 'HelloWorld')  结果:elloWorld

还有LTrimRTrim,可以指定左右去除的字符内容

例如:

Trim、LTrim、RTrim函数

select Trim ('H' FROM 'HelloWorld'),

LTrim('happybirthday','ha'),

RTrim('happybirthday','thday')

from dual;

Replace 将…替换为…Replace (‘abcd’,’b’,’m’)  结果:amcd

Round 四舍五入  例如:Round (11.3578, 2)   →           11.36

Trunc 截断      例如:Trunc (11.3578, 2)    →           11.35 

Mod 求余       例如:Mod (70, 30)             →          10

举例说明:Replace、Round、Trunc、Mod函数

select Replace('abcd','b','m'),Round(11.3578, 2),

Trunc (11.3578, 2),Mod (70, 30)

from dual --dual是伪表;

Months_between两个日期相差的月数

Add_months向指定日期中加上若干月数

Next_day指定日期的下一个星期 * 对应的日期

Last_day本月的最后一天

举例说明:Months_between、Add_months、Next_day、Last_day函数

select hire_date,

months_between(sysdate,hire_date) as months_between,

next_day(hire_date,'星期一') as next_day,

Add_months(hire_date,6) as Add_months,

last_day(hire_date) as last_day

from employees

 

to_date、to_char、to_number函数

select to_date('2015-3-18 13:13:13','yyyy-mm""-dd hh24:mi:ss') as "date年月日",

to_date('1-1-2000') as "date日期",

to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as "char时分秒",

to_char(salary,'L999,999.99') as  "char金钱",

to_char(hire_date,'DAY') as  "char星期",

to_char(manager_id) as  "char字符",

to_char(hire_date,'mm"月份/"yyyy"年份"') as  "char日期",

to_number('1,234,567,890.00','L999,999,999,999.99')

from employees

NVL格式:NVL2 (expr1, expr2) : expr1不为NULL,返回expr2

NVL2格式:NVL2 (expr1, expr2, expr3) : expr1不为NULL,返回expr2;为NULL,返回expr3

举例说明:NVL、NVL2函数

select concat(last_name,first_name),

NVL(to_char(manager_id),'No Manager') as Manager_id,

NVL2(commission_pct, 'salary+comm', 'salary') income

from employees

 

举例说明:NULLIF函数

select length(salary) "one",

       length(job_id)  "two",

       NULLIF(length(salary), length(job_id)) result

from   employees;

举例说明:coalese函数

SELECT   commission_pct,manager_id,salary,

COALESCE(commission_pct, manager_id, salary) commission

FROM     employees

ORDER BY salary

 

条件表达式:case…when…then

select salary,

case when salary < 1000 then salary*0

when  salary> 1000 and salary<2000 then salary*0.1

when  salary>= 2000 and salary<=3000 then salary*0.15

when  salary >=3000 then salary*0.2

else salary end

from employees

 
  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值