数据库中:字符串 转换为 时间格式

二者区别:

to_data 转换为 普通的时间格式
        to_timestamp 转换可为 时间戳格式
出错场景: 比较同一天 日期大小的时候,很容易出错

例如:
        select current_timestamp from pub_employee
        结果如下:
        
    select current_timestamp <= to_date('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') flag from pub_employee
    语句中的2018-03-12 18:47:35 要比 current_timestamp当前的时间 大两个小时,
    但是结果如下:

结果是 false
原因是:select to_date('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') from pub_employee
的结果如下:并不是时间戳


正确的写法
select current_timestamp <= to_timestamp('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') flag from pub_employee
结果:
为true
因为:select to_timestamp('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') from pub_employee

============================================================
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

使用to_date 返回的都是以下结果:


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
方式一和二都是以下格式,虽然都是时间戳,但是后面一截是0

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

---------------------
作者:大bug
来源:CSDN
原文:https://blog.csdn.net/sky_limitless/article/details/79527665
版权声明:本文为博主原创文章,转载请附上博文链接!

最新文章

  1. SharePoint2016合规性策略中心
  2. Jquery datatables 使用方法
  3. Objective-C( Foundation框架 一 NSFileManager)
  4. HTML 学习笔记(块 和 类)
  5. jquery 设置焦点
  6. android属性之excludeFromRecents -- clearTaskOnLaunch 隐身意图 启动activity
  7. 【转】C#综合揭秘——通过修改注册表建立Windows自定义协议
  8. 使用C#三维图形控件进行曲线曲面分析
  9. HTML xmlns
  10. Centos 7 静态学习IP建立
  11. Python资源汇总
  12. 解决Perhaps you are running on a JRE rather than a JDK?问题
  13. Snapde一个全新的CSV超大文件编辑软件
  14. JavaScript模块化思想之CommonJS、AMD、CMD、UMD
  15. gradle3.0新命令
  16. R语言之Random Forest随机森林
  17. C# 给某个方法设定执行超时时间
  18. Java基础学习——多线程之线程池
  19. js判断手机型号
  20. Struts2,Spring3,Hibernate4整合--SSH框架

热门文章

  1. AJAX中所谓的异步
  2. Django 之form简单应用
  3. 关于dataset
  4. Spring Cloud(一)服务的注册与发现(Eureka)
  5. WPF实战案例-在线程内同步集合数据到UI线程
  6. Dart中的匿名方法与自执行方法
  7. 5.安装bacula-web(监控页面)
  8. 如何用win10自带linux系统学习c语言---解决gdb使用问题
  9. 简单的理解 StringBuffer/StringBuilder/String 的区别
  10. spark为什么比hadoop的mr要快?