postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1985字,纯文字阅读大概需要3分钟。
内容图文
数据库中:字符串 转换为 时间格式
二者区别:
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
版权声明:本文为博主原创文章,转载请附上博文链接!
postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式
标签:sql数据库 bug 写法 作者 声明 nbsp from inf timestamp
本文系统来源:https://www.cnblogs.com/telwanggs/p/11056500.html
内容总结
以上是互联网集市为您收集整理的postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式全部内容,希望文章能够帮你解决postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。