oracle中的单行函数笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/czh500/article/details/82599303
--dual是一个伪表,可以用来测试函数和表达式

select lower('JiAngXiShengGAnZHouSHiYUdUXiAn'), upper('CZH hahaABCDEFGhjklmnopq'), initcap('love chiNa okok gogo JkmnOpERlki') from dual;

函数 lower('JiAngXiShengGAnZHouSHiYUdUXiAn') --全部变成小写

函数 upper('CZH hahaABCDEFGhjklmnopq') --全部变成大写

函数 initcap('love chiNa okok gogo JkmnOpERlki') --把每个单词的首字母变成大写

select * from employees where lower(last_name) = 'king';

select * from employees where upper(last_name) = 'KING';

select concat('江西省', '赣州市于都县'), substr('江西省赣州市于都县', 2, 7), length('江西省赣州市于都县') from dual;

concat('江西省', '赣州市于都县')函数 -- 将2个字符串连接起来
substr('江西省赣州市于都县', 2, 7)函数 --截取字符串(注意:sql中下标是从1开始的,java中的下标是从0开始的,这里的2表示从下标2开始,7表示截取7个字符)
length('江西省赣州市于都县')函数 --得到字符串长度


select instr('江西省赣州市于都县', '于') from dual;

instr('江西省赣州市于都县', '都')函数 --都首次出现的位置

instr('江西省赣州市于都县', '我')函数 --我首次出现的位置,如果没有找到我字,那就返回0(注意:在java中要是没有找到的话,返回-1,而sql中没找到的话,返回0)

select salary, lpad(salary, 10, '*'), rpad(salary, 10, '*') from employees;

lpad(salary, 10, '*')函数 --总共占10个位置,不够的话,用*号来补充,补充在左边

rpad(salary, 10, '*')函数 --总共占10个位置,不够的话,用*号来补充,补充在右边


select trim('8' from '888江西省888赣州市888于都县渡江大道888号8578908'), replace('66江西省66于都县666888999号66', '6', 'A') from dual;

trim('8' from '888江西省888赣州市888于都县渡江大道888号8578908')函数 --trim()函数只能把首尾的8去除,中间的8不能去除
replace('66江西省66于都县666888999号66', '6', 'A')函数 --replace()函数是把所有的6都替换成A


--round(5678.985, 2)保留2位小数,round(5678.985)等同于保留0位小数,round(5678.985, -2)保留到十位,注意:这里都是四舍五入
select round(5678.985, 2), round(5678.985), round(5678.985, -2) from dual; --结果是 5678.99   5679  5700   

--trunc()是截断,不会四舍五入
select trunc(5678.985, 2), trunc(5678.985), trunc(5678.985, -2) from dual; --结果是5678.98  5678  5600

select mod(110, 30) from dual;--mod(110, 30)求余函数

select sysdate, sysdate + 1, sysdate -3 from dual;

--日期之间是不能做加法运算的,日期之间相加毫无意义(日期跟日期之间仅能做减法)

select months_between(sysdate, hire_date) from employees;--months_between(sysdate, hire_date)函数,2个日期相差的月数

--add_months(sysdate, 2)函数在当前日期基础上加2个月,next_day(sysdate, '星期日')函数得到最近的星期日是哪一天
select add_months(sysdate, 2), add_months(sysdate, -3), next_day(sysdate, '星期日') from dual;

--last_day()函数每个月的最后一天
select last_name, hire_date from employees where hire_date = last_day(hire_date) - 1;
select last_day(sysdate) from dual;


select round(sysdate, 'month'), round(sysdate, 'mm'), round(sysdate, 'hh'), trunc(sysdate, 'hh') from dual;
select round(sysdate, 'month'), round(sysdate, 'mm'), round(sysdate, 'hh'), round(sysdate, 'yyyy'), round(sysdate, 'year'), trunc(sysdate, 'hh') from dual;

--    date(日期类型)<------>varchar2(字符/字符串类型)<------>number(数字类型) 这3者之间可以相互转换(隐式转换/自动转换、显示转换)

select '16' + 2 from dual; --结果是18(注意:在java中一个字符串用+号加上一个数字,那结果就是将2部分连接起来,结果是一个162的字符串,而在oracle数据库
中,+加号就是做运算,系统会自动把'16'这个字符串转换成一个number数字类型的,然后在和数字2相加,最终得到结果18,如果要把字符串'16'和数字2连接起来的话,使用||)


--使用||(||号是字符之间的连接,比如 '123'|| '6688'得到的结果是1236688,类似于java中的多个字符串连接在一起)和使用+(+号是做加的算数运算)


select sysdate + '2' from dual; -- 隐式转换/自动转换


--显式数据类型转换

select last_name, hire_date from employees where to_char(hire_date, 'yyyy-mm-dd') = '1994-06-07';
select last_name, hire_date from employees where to_char(hire_date, 'yyyy/mm/dd') = '1994/06/07';
select last_name, hire_date from employees where to_date('1994-06-07', 'yyyy-mm-dd') = hire_date;
select last_name, hire_date from employees where hire_date = to_date('1994-06-07', 'yyyy-mm-dd');

--注意:要用双引号把年月日这3个汉字包起来
select last_name, to_char(hire_date, 'yyyy"年"mm"月"dd"日"') from employees where to_char(hire_date, 'yyyy"年"mm"月"dd"日"') = '1994年06月07日';

--将number数字类型转换成字符串类型
select to_char(1234567.89, '999,999,999.999') from dual; --结果是1,234,567.890

select to_char(1234567.89, '999,000,999.999') from dual; --结果是1,234,567.890

select to_char(1234567.89, '000,999,999.999') from dual; --结果是001,234,567.890

select to_char(1234567.89, 'L000,999,999.999') from dual; --结果是¥001,234,567.890  (注意:L表示本地的货币符号)

select to_char(1234567.89, '$999,999,999.999') from dual; --结果是$1,234,567.890


--将字符串类型转换成number数字类型

select to_number('¥001,234,567.890', 'L000,999,999.999') from dual; 结果是1234567.89
select to_number('$001,234,567.890', '$000,999,999.999') from dual; 结果是1234567.89


--通用函数(适用于任何数据类型,同时也适用于空值)
select last_name, salary, nvl(commission_pct, 0) from employees;--nvl(p1,p2)函数,如果p1为空的为就用p2来代替空

--department_id列是number数字类型的,没有部门这4个汉字是字符类型的,这2种类型不兼容,所以需要先将department_id转换成字符类型的
select last_name, department_id, nvl(to_char(department_id, '999999999'), '没有部门') from employees;
select last_name, department_id, nvl(to_char(department_id), '没有部门') from employees;

--nvl(p1,p2,p3)函数,如果p1为空,那么就显示p3,如果p1不为空的话,那就显示p2
select last_name, commission_pct, nvl2(commission_pct, commission_pct + 0.015, 0.01) from employees;

--nullif(p1,p2)函数,如果p1和p2相等的话,返回null,如果p1和p2不相等的话,返回p1
select last_name, length(last_name) as "name1", first_name, length(first_name) as "name2", nullif(length(last_name), length(first_name)) as "result" from employees;

--coalesce(p1, p2, p3, p4 ......)函数,如果p1为空,那就显示p2,如果p2为空,那就显示p3,以此类推
select last_name, department_id, commission_pct, coalesce(department_id, commission_pct, 88888888) as "result" from employees;


--条件表达式
-- case when ... then ... else ... end  (case表达式)
select last_name, department_id, salary, case department_id  
    when 10 then salary * 1.1
    when 20 then salary * 1.2
    else salary * 1.3 end as "new_salary"
    from employees where department_id in (10, 20, 30);

--使用decode()函数的方式
select last_name, department_id, salary, decode(department_id,
10, salary * 1.1,
20, salary * 1.2,
salary * 1.3
)
as "new_salary"
from employees
where department_id in (10, 20, 30);

--结果是2018年09月11日 00:27:31
select to_char(sysdate, 'yyyy"年"mm"月"dd"日" hh24:mi:ss') from dual;
--结果是2018年09月11日 12:27:31
select to_char(sysdate, 'yyyy"年"mm"月"dd"日" hh:mi:ss') from dual;

--将数字转换成字符串
select to_char(1234567.89, '999,999,999.999') from dual;

--若字符串中没有特殊字符,可以进行隐式转换
select '123456' + 10 from dual;

--如果字符串中有特殊字符,如1,234,567.89,则无法进行隐式转换,需要使用to_number()函数来完成
select to_number('1,234,567.890', '999,999,999.999') + 10 from dual;

--dual是一个伪表,可以用来测试函数和表达式

select lower('JiAngXiShengGAnZHouSHiYUdUXiAn'), upper('CZH hahaABCDEFGhjklmnopq'), initcap('love chiNa okok gogo JkmnOpERlki') from dual;

函数 lower('JiAngXiShengGAnZHouSHiYUdUXiAn') --全部变成小写

函数 upper('CZH hahaABCDEFGhjklmnopq') --全部变成大写

函数 initcap('love chiNa okok gogo JkmnOpERlki') --把每个单词的首字母变成大写

select * from employees where lower(last_name) = 'king';

select * from employees where upper(last_name) = 'KING';

select concat('江西省', '赣州市于都县'), substr('江西省赣州市于都县', 2, 7), length('江西省赣州市于都县') from dual;

concat('江西省', '赣州市于都县')函数 -- 将2个字符串连接起来
substr('江西省赣州市于都县', 2, 7)函数 --截取字符串(注意:sql中下标是从1开始的,java中的下标是从0开始的,这里的2表示从下标2开始,7表示截取7个字符)
length('江西省赣州市于都县')函数 --得到字符串长度


select instr('江西省赣州市于都县', '于') from dual;

instr('江西省赣州市于都县', '都')函数 --都首次出现的位置

扫描二维码关注公众号,回复: 3279950 查看本文章

instr('江西省赣州市于都县', '我')函数 --我首次出现的位置,如果没有找到我字,那就返回0(注意:在java中要是没有找到的话,返回-1,而sql中没找到的话,返回0)

select salary, lpad(salary, 10, '*'), rpad(salary, 10, '*') from employees;

lpad(salary, 10, '*')函数 --总共占10个位置,不够的话,用*号来补充,补充在左边

rpad(salary, 10, '*')函数 --总共占10个位置,不够的话,用*号来补充,补充在右边


select trim('8' from '888江西省888赣州市888于都县渡江大道888号8578908'), replace('66江西省66于都县666888999号66', '6', 'A') from dual;

trim('8' from '888江西省888赣州市888于都县渡江大道888号8578908')函数 --trim()函数只能把首尾的8去除,中间的8不能去除
replace('66江西省66于都县666888999号66', '6', 'A')函数 --replace()函数是把所有的6都替换成A


--round(5678.985, 2)保留2位小数,round(5678.985)等同于保留0位小数,round(5678.985, -2)保留到十位,注意:这里都是四舍五入
select round(5678.985, 2), round(5678.985), round(5678.985, -2) from dual; --结果是 5678.99   5679  5700   

--trunc()是截断,不会四舍五入
select trunc(5678.985, 2), trunc(5678.985), trunc(5678.985, -2) from dual; --结果是5678.98  5678  5600

select mod(110, 30) from dual;--mod(110, 30)求余函数

select sysdate, sysdate + 1, sysdate -3 from dual;

--日期之间是不能做加法运算的,日期之间相加毫无意义(日期跟日期之间仅能做减法)

select months_between(sysdate, hire_date) from employees;--months_between(sysdate, hire_date)函数,2个日期相差的月数

--add_months(sysdate, 2)函数在当前日期基础上加2个月,next_day(sysdate, '星期日')函数得到最近的星期日是哪一天
select add_months(sysdate, 2), add_months(sysdate, -3), next_day(sysdate, '星期日') from dual;

--last_day()函数每个月的最后一天
select last_name, hire_date from employees where hire_date = last_day(hire_date) - 1;
select last_day(sysdate) from dual;


select round(sysdate, 'month'), round(sysdate, 'mm'), round(sysdate, 'hh'), trunc(sysdate, 'hh') from dual;
select round(sysdate, 'month'), round(sysdate, 'mm'), round(sysdate, 'hh'), round(sysdate, 'yyyy'), round(sysdate, 'year'), trunc(sysdate, 'hh') from dual;

--    date(日期类型)<------>varchar2(字符/字符串类型)<------>number(数字类型) 这3者之间可以相互转换(隐式转换/自动转换、显示转换)

select '16' + 2 from dual; --结果是18(注意:在java中一个字符串用+号加上一个数字,那结果就是将2部分连接起来,结果是一个162的字符串,而在oracle数据库
中,+加号就是做运算,系统会自动把'16'这个字符串转换成一个number数字类型的,然后在和数字2相加,最终得到结果18,如果要把字符串'16'和数字2连接起来的话,使用||)


--使用||(||号是字符之间的连接,比如 '123'|| '6688'得到的结果是1236688,类似于java中的多个字符串连接在一起)和使用+(+号是做加的算数运算)


select sysdate + '2' from dual; -- 隐式转换/自动转换

--显式数据类型转换

select last_name, hire_date from employees where to_char(hire_date, 'yyyy-mm-dd') = '1994-06-07';
select last_name, hire_date from employees where to_char(hire_date, 'yyyy/mm/dd') = '1994/06/07';
select last_name, hire_date from employees where to_date('1994-06-07', 'yyyy-mm-dd') = hire_date;
select last_name, hire_date from employees where hire_date = to_date('1994-06-07', 'yyyy-mm-dd');

--注意:要用双引号把年月日这3个汉字包起来
select last_name, to_char(hire_date, 'yyyy"年"mm"月"dd"日"') from employees where to_char(hire_date, 'yyyy"年"mm"月"dd"日"') = '1994年06月07日';

--将number数字类型转换成字符串类型
select to_char(1234567.89, '999,999,999.999') from dual; --结果是1,234,567.890

select to_char(1234567.89, '999,000,999.999') from dual; --结果是1,234,567.890

select to_char(1234567.89, '000,999,999.999') from dual; --结果是001,234,567.890

select to_char(1234567.89, 'L000,999,999.999') from dual; --结果是¥001,234,567.890  (注意:L表示本地的货币符号)

select to_char(1234567.89, '$999,999,999.999') from dual; --结果是$1,234,567.890


--将字符串类型转换成number数字类型

select to_number('¥001,234,567.890', 'L000,999,999.999') from dual; 结果是1234567.89
select to_number('$001,234,567.890', '$000,999,999.999') from dual; 结果是1234567.89


--通用函数(适用于任何数据类型,同时也适用于空值)
select last_name, salary, nvl(commission_pct, 0) from employees;--nvl(p1,p2)函数,如果p1为空的为就用p2来代替空

--department_id列是number数字类型的,没有部门这4个汉字是字符类型的,这2种类型不兼容,所以需要先将department_id转换成字符类型的
select last_name, department_id, nvl(to_char(department_id, '999999999'), '没有部门') from employees;
select last_name, department_id, nvl(to_char(department_id), '没有部门') from employees;

--nvl(p1,p2,p3)函数,如果p1为空,那么就显示p3,如果p1不为空的话,那就显示p2
select last_name, commission_pct, nvl2(commission_pct, commission_pct + 0.015, 0.01) from employees;

--nullif(p1,p2)函数,如果p1和p2相等的话,返回null,如果p1和p2不相等的话,返回p1
select last_name, length(last_name) as "name1", first_name, length(first_name) as "name2", nullif(length(last_name), length(first_name)) as "result" from employees;

--coalesce(p1, p2, p3, p4 ......)函数,如果p1为空,那就显示p2,如果p2为空,那就显示p3,以此类推
select last_name, department_id, commission_pct, coalesce(department_id, commission_pct, 88888888) as "result" from employees;


--条件表达式
-- case when ... then ... else ... end  (case表达式)
select last_name, department_id, salary, case department_id  
    when 10 then salary * 1.1
    when 20 then salary * 1.2
    else salary * 1.3 end as "new_salary"
    from employees where department_id in (10, 20, 30);

--使用decode()函数的方式
select last_name, department_id, salary, decode(department_id,
10, salary * 1.1,
20, salary * 1.2,
salary * 1.3
)
as "new_salary"
from employees
where department_id in (10, 20, 30);

--结果是2018年09月11日 00:27:31
select to_char(sysdate, 'yyyy"年"mm"月"dd"日" hh24:mi:ss') from dual;
--结果是2018年09月11日 12:27:31
select to_char(sysdate, 'yyyy"年"mm"月"dd"日" hh:mi:ss') from dual;

--将数字转换成字符串
select to_char(1234567.89, '999,999,999.999') from dual;

--若字符串中没有特殊字符,可以进行隐式转换
select '123456' + 10 from dual;

--如果字符串中有特殊字符,如1,234,567.89,则无法进行隐式转换,需要使用to_number()函数来完成
select to_number('1,234,567.890', '999,999,999.999') + 10 from dual;

猜你喜欢

转载自blog.csdn.net/czh500/article/details/82599303
今日推荐