Oracle中的to_date和to_char用法总结

Oracle提供的单行函数有两个比较重要的函数to_date和to_char,通过案例掌握用法:

to_char用法

1.语法:to_char(日期,'日期格式‘)
2.作用:将日期通过指定格式进行字符串转换,转换成字符串类型
3.案例:
1.

  //将当前系统时间转换成(yyyy-mm-dd hh:mi:ss day)格式。
    `select to_char(sysdate , 'yyyy-mm-dd hh:mi:ss day') from dual;` 
 把hiredate列看做是员工的生日,查询本月过生日的员工(考察知识点:单行函数)
 select * from employees where to_char(hire_date,'mm')=to_char(sysdate,'mm');

to_date用法

1.语法:to_date(字符串,'日期格式’)
2.用法:将字符串通过指定格式进行日期转换,转换成日期类型
3.案例:

  //将和日期兼容的字符串按照(yyyy-mm-dd hh24:mi:ss:ff day)转换成日期。
     select to_date('19950101','yyyy-mm-dd') from dual;
  1. 查询入职日期在1997-5-1到1997-12-31之间的所有员工信息 select * from employees where to_date('1997-5-1','yyyy-mm-dd')hire_date;

综合案例:
3. 请用三种以上的方式查询2002年入职的员工(考察知识点:单行函数)提示:like to_char提取年 直接>2002年一月一号

select * from employees where hire_date like '2002%';select * from employees where to_char(hire_date,'yyyy')=2002;select * from employees where to_char(hire_date,'yyyymmdd')>20020101 and to_char(hire_date,'yyyymmdd')<20021231;
  1. 查询2002年下半年入职的员工(考察知识点:单行函数)提示:提取年为2002,提取月大于6

select * from employees where to_char(hire_date,‘yyyy’)=2002 and to_char(hire_date,‘mm’)>6 and to_char(hire_date,‘mm’)<=12;

5.打印自己出生了多少天

select sysdate-to_date('1996/10/16','yyyy/mm/dd') from dual;

6.出生那天星期几?

select to_char(to_date('1996/10/16','yyyy/mm/dd'),'yyyy-mm-dd day') from dual;//星期三

你可能感兴趣的:(Oracle,Java,web,Oracle,to_date,to_char,案例)