--日期和字符转换函数用法
--(to_date,to_char)
--日期转换为字符串
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;
--获取时间的年
select to_char(sysdate,'yyyy') as nowYear from dual;
--获取时间的月
select to_char(sysdate,'mm')as nowMonth from dual;
--获取时间的日
select to_char(sysdate,'dd')as nowDay from dual;
--获取时间的时
select to_char(sysdate,'hh24')as nowHour from dual;
--获取时间的分
select to_char(sysdate,'mi')as nowMinute from dual;
--获取时间的秒
select to_char(sysdate,'ss')as nowSecond from dual; 、
--求某天是星期几
select to_char(to_date('1998-04-27','yyyy-mm-dd'),'day') from dual;
--设置日期语言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
--两个日期间的天数
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
--时间为null的用法-------------
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual;
--月份差
select months_between(to_date('1998-4-27','yyyy-MM-dd'),to_date('1995-7-30','yyyy-MM-dd')) from dual;
--一年的第一天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
--返回日期列表中最晚日期
select greatest('01-1月-04','04-1月-04','10-2月-04') from dual
--计算时间差
--时间差-年
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual
--时间差-月
select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual
--时间差-天
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual
--时间差-时
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual
--时间差-分
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual
--时间差-秒
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual
--查找月的第一天,最后一天
SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,
LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month
FROM dual;
--字符函数
--1.字符串截取
select substr('abcdef',1,3) from dual
--2.查找子串位置
select instr('abcfdgfdhd','fd') from dual
--3.字符串连接
select 'HELLO'||'hello world' from dual;
--4.去掉字符串中的空格
select ltrim(' abc') s1,
rtrim('zhang ') s2,
trim(' zhang ') s3 from dual
--5.去掉前导和后缀
select trim(leading 9 from 9998767999) s1,
trim(trailing 9 from 9998767999) s2,
trim(9 from 9998767999) s3 from dual;
--6.返回字符串首字母的Ascii值
select ascii('a') from dual
--7.返回ascii值对应的字母
select chr(97) from dual
--8.计算字符串长度
select length('abcdef') from dual
--9.initcap(首字母变大写) ,lower(变小写),upper(变大写)
select lower('ABC') s1,
upper('def') s2,
initcap('efg') s3
from dual;
--数字函数
--1.取整函数(ceil 向上取整,floor 向下取整)
select ceil(66.6) N1,floor(66.6) N2 from dual;
--2. 取幂(power) 和 求平方根(sqrt)
select power(3,2) N1,sqrt(9) N2 from dual;
--3.求余
select mod(9,5) from dual;
--4.返回固定小数位数 (round:四舍五入,trunc:直接截断)
select round(66.667,2) N1,trunc(66.667,2) N2 from dual;
--5.返回值的符号(正数返回为1,负数为-1)
select sign(-32),sign(293) from dual;
--转换函数
--to_char()[将日期和数字类型转换成字符类型
--1.
select to_char(sysdate) s1,
to_char(sysdate,'yyyy-mm-dd') s2,
to_char(sysdate,'yyyy') s3,
to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') s4,
to_char(sysdate, 'hh24:mi:ss') s5,
to_char(sysdate,'DAY') s6
from dual;
--2.
--select sal,to_char(sal,'$99999') n1,to_char(sal,'$99,999') n2 from emp
--3.to_date()[将字符类型转换为日期类型]
--insert into emp(empno,hiredate) values(8000,to_date('2004-10-10','yyyy-mm-dd'));
--4. to_number() 转换为数字类型 //以数字显示的小时数
select to_number(to_char(sysdate,'hh12')) from dual;
--其它函数
--1.user: 返回登录的用户名称
select user from dual;
--2.vsize: 返回表达式所需的字节数
select vsize('HELLO') from dual;
--3.3.nvl(ex1,ex2):
--ex1值为空则返回ex2,否则返回该值本身ex1(常用)
select comm,nvl(comm,0) from emp;
--4.nullif(ex1,ex2):
--值相等返空,否则返回第一个值
select nullif(sal,comm),sal,comm from emp;
--5.coalesce:
--返回列表中第一个非空表达式
select comm,sal,coalesce(comm,sal,sal*10) from emp;
--分组函数
--1. 整个结果集是一个组
--(to_date,to_char)
--日期转换为字符串
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;
--获取时间的年
select to_char(sysdate,'yyyy') as nowYear from dual;
--获取时间的月
select to_char(sysdate,'mm')as nowMonth from dual;
--获取时间的日
select to_char(sysdate,'dd')as nowDay from dual;
--获取时间的时
select to_char(sysdate,'hh24')as nowHour from dual;
--获取时间的分
select to_char(sysdate,'mi')as nowMinute from dual;
--获取时间的秒
select to_char(sysdate,'ss')as nowSecond from dual; 、
--求某天是星期几
select to_char(to_date('1998-04-27','yyyy-mm-dd'),'day') from dual;
--设置日期语言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
--两个日期间的天数
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
--时间为null的用法-------------
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual;
--月份差
select months_between(to_date('1998-4-27','yyyy-MM-dd'),to_date('1995-7-30','yyyy-MM-dd')) from dual;
--一年的第一天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
--返回日期列表中最晚日期
select greatest('01-1月-04','04-1月-04','10-2月-04') from dual
--计算时间差
--时间差-年
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual
--时间差-月
select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual
--时间差-天
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual
--时间差-时
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual
--时间差-分
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual
--时间差-秒
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual
--查找月的第一天,最后一天
SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,
LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month
FROM dual;
--字符函数
--1.字符串截取
select substr('abcdef',1,3) from dual
--2.查找子串位置
select instr('abcfdgfdhd','fd') from dual
--3.字符串连接
select 'HELLO'||'hello world' from dual;
--4.去掉字符串中的空格
select ltrim(' abc') s1,
rtrim('zhang ') s2,
trim(' zhang ') s3 from dual
--5.去掉前导和后缀
select trim(leading 9 from 9998767999) s1,
trim(trailing 9 from 9998767999) s2,
trim(9 from 9998767999) s3 from dual;
--6.返回字符串首字母的Ascii值
select ascii('a') from dual
--7.返回ascii值对应的字母
select chr(97) from dual
--8.计算字符串长度
select length('abcdef') from dual
--9.initcap(首字母变大写) ,lower(变小写),upper(变大写)
select lower('ABC') s1,
upper('def') s2,
initcap('efg') s3
from dual;
--数字函数
--1.取整函数(ceil 向上取整,floor 向下取整)
select ceil(66.6) N1,floor(66.6) N2 from dual;
--2. 取幂(power) 和 求平方根(sqrt)
select power(3,2) N1,sqrt(9) N2 from dual;
--3.求余
select mod(9,5) from dual;
--4.返回固定小数位数 (round:四舍五入,trunc:直接截断)
select round(66.667,2) N1,trunc(66.667,2) N2 from dual;
--5.返回值的符号(正数返回为1,负数为-1)
select sign(-32),sign(293) from dual;
--转换函数
--to_char()[将日期和数字类型转换成字符类型
--1.
select to_char(sysdate) s1,
to_char(sysdate,'yyyy-mm-dd') s2,
to_char(sysdate,'yyyy') s3,
to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') s4,
to_char(sysdate, 'hh24:mi:ss') s5,
to_char(sysdate,'DAY') s6
from dual;
--2.
--select sal,to_char(sal,'$99999') n1,to_char(sal,'$99,999') n2 from emp
--3.to_date()[将字符类型转换为日期类型]
--insert into emp(empno,hiredate) values(8000,to_date('2004-10-10','yyyy-mm-dd'));
--4. to_number() 转换为数字类型 //以数字显示的小时数
select to_number(to_char(sysdate,'hh12')) from dual;
--其它函数
--1.user: 返回登录的用户名称
select user from dual;
--2.vsize: 返回表达式所需的字节数
select vsize('HELLO') from dual;
--3.3.nvl(ex1,ex2):
--ex1值为空则返回ex2,否则返回该值本身ex1(常用)
select comm,nvl(comm,0) from emp;
--4.nullif(ex1,ex2):
--值相等返空,否则返回第一个值
select nullif(sal,comm),sal,comm from emp;
--5.coalesce:
--返回列表中第一个非空表达式
select comm,sal,coalesce(comm,sal,sal*10) from emp;
--分组函数
--1. 整个结果集是一个组