Oracle中的to_date和to_char用法总结

博客介绍了Oracle中to_date和to_char两个重要单行函数。to_char可将日期按指定格式转为字符串,to_date能将字符串按指定格式转为日期。还给出综合案例,如查询2002年入职员工、2002年下半年入职员工,以及计算出生天数、出生星期等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 and to_date('1997-12-31','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;//星期三
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mayz梅子子子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值