sql字符和日期函数

大小写转换函数UPPER:转换成大写 lower:转换成小写 initcap:第一个字母变为大写
select employee_id,first_name,UPPER(first_name),lower(first_name),initcap(first_name)
from employees;
sql字符和日期函数
单行函数可用于选择排序依据
select employee_id,first_name from employees
where UPPER(first_name)=‘PATRICK’;
sql字符和日期函数
concat函数(把两个列连接到一块)
select employee_id,last_name,first_name,concat(last_name,first_name)
from employees;
sql字符和日期函数
select employee_id,last_name,first_name,last_name||’ ‘||first_name||salary
from employees;
sql字符和日期函数
字符截取函数substr
substr(first_name,1,3):从第一个字符截取,截取三位
substr(first_name,2,4):从第二个字符截取,截取四位
substr(first_name,2):从第二个字符截取,截取到最后
substr(first_name,-3):倒着截取三位
select employee_id,first_name,
substr(first_name,1,3),substr(first_name,2,4),substr(first_name,2),substr(first_name,-3)
from employees;
sql字符和日期函数
length:获取字符串长度
sql字符和日期函数
Lpad:从左边对字符串使用指定的字符进行填充
rpad:从左边对字符串使用指定的字符进行填充
sql字符和日期函数
替换函数
把字母a替换成** 把字母带en替换成#
select employee_id,first_name,
replace(first_name,‘a’,’**’),replace(first_name,‘en’,’#’)
from employees;
sql字符和日期函数
数值函数(最不爱学的地方)
四舍五入:截取的小数要四舍五入
sql字符和日期函数
select round(10.49) from dual;
sql字符和日期函数
select round(10.499, 1) from dual;
sql字符和日期函数
select round(10.493, 2) from dual;
sql字符和日期函数
select round(10.493, -1) from dual;
sql字符和日期函数
select round(10.493, -2) from dual;
sql字符和日期函数
select round(10.493, -3) from dual;
sql字符和日期函数
Trunc:它只负责截取,不管你四舍五入
select trunc(10.6565 ) from dual;
sql字符和日期函数
select trunc(10.6565 ,2 ) from dual;(保留小数点2位)
sql字符和日期函数
如果为负数,则先保留整数部分,然后从个位开始向前数,并将遇到的数字都变为0。
select trunc(998.6565,-2) from dual;
sql字符和日期函数
select trunc(998.6565,-3) from dual; 后两位变成0
sql字符和日期函数
Mod:余数
select mod(15,2) from dual;
sql字符和日期函数
select mod(15,3) from dual;
sql字符和日期函数
select mod(100,2) from dual;
sql字符和日期函数
select mod(101,2) from dual;
sql字符和日期函数
sql字符和日期函数
sql字符和日期函数
sql字符和日期函数
获取当前日期
select sysdate from dual;
sql字符和日期函数
select sysdate, sysdate +3 from dual; 三天后的时间
sql字符和日期函数
select sysdate, sysdate -3 from dual; 三天前的时间
sql字符和日期函数
当前日期-过去时间=过去了多少天
select employee_id,sysdate,hire_date,SYSDATE-hire_date,round(SYSDATE-hire_date)
from employees;
sql字符和日期函数
总工作时间以周计算:(SYSDATE-hire_date)/7
select employee_id,sysdate,hire_date,SYSDATE-hire_date,(SYSDATE-hire_date)/7
from employees;
sql字符和日期函数
MONTHS_BETWEEN:返回两个日期间隔的月数
select employee_id,first_name,MONTHS_BETWEEN(sysdate,hire_date),(sysdate-hire_date)/30
from employees;
sql字符和日期函数
4个月后的日期
select employee_id,first_name,hire_date,add_months(hire_date,4) from employees;
sql字符和日期函数
2个月的前的日期
select employee_id,first_name,hire_date,add_months(hire_date,-2) from employees;
sql字符和日期函数
NEXT_DAY:显示下周星期(1到7)期是几号
select sysdate,NEXT_DAY(sysdate,‘星期一’)from dual;
sql字符和日期函数
sql字符和日期函数
指定成数字
select sysdate,NEXT_DAY(sysdate,4)from dual;
sql字符和日期函数
sql字符和日期函数
last_day:月份的最后一天
select last_day(sysdate) from dual;
sql字符和日期函数
select employee_id,first_name,hire_date,sysdate,
MONTHS_BETWEEN(sysdate,hire_date),add_months(hire_date,6),next_day(hire_date,‘星期五’)
from employees;
sql字符和日期函数
ROUND:对日期进行四舍五入操作,按照年(YEAR)月(MONTH)日(DAY)
Trunk: 对日期进行截取操作,按照年(YEAR)月(MONTH)日(DAY)
round对月进行计算: 对月份,按照1-15,15-31日进行四舍五入
select employee_id,first_name,hire_date,
round(hire_date,‘month’),trunc(hire_date,‘month’)
from employees;
sql字符和日期函数
Round:对年份,按照1-6月,7-12月进行四舍五入
select employee_id,first_name,hire_date,
round(hire_date,‘year’),trunc(hire_date,‘year’)
from employees;
sql字符和日期函数