sql to_date && to_timestamp

1.二者区别:

        to_data 转换为 普通的时间格式

        to_timestamp 转换可为 时间戳格式

2.使用误区:

比较同一天 日期大小的时候

    select current_timestamp <= to_date('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') from pub_employee

执行该sql不一定获取全部结果

原因:

select to_date('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') from pub_employee

的结果并不是时间戳,而是2018-03-12

正确的写法

select current_timestamp <= to_timestamp('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') from pub_employee

3.to_date:


方式一:正确
select to_date('2018-03-08','yyyy-MM-dd') from pub_employee
方式二:
select to_date('2018-03-08 18:55:33','yyyy-MM-dd') from pub_employee
方式三:
select to_date('2018-03-08 18:55:33','yyyy-MM-dd hh24:mi:ss') from pub_employee

返回的均为2018-03-08

4.to_timestamp:


方式一:
select to_timestamp('2018-03-08','yyyy-MM-dd') from pub_employee
方式二:
select to_timestamp('2018-03-08 18:55:33','yyyy-MM-dd') from pub_employee
方式一和二都是以下格式,虽然都是时间戳,但是后面一截是2018-03-08 00:00:00

方式三:正确
select to_timestamp('2018-03-08 18:55:33','yyyy-MM-dd hh24:mi:ss') from pub_employee
 


转自:https://blog.csdn.net/sky_limitless/article/details/79527665

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值