todate日期格式写法(PG数据库的todate和totimestamp 将字符串转换为时间格式)

作者:啦啦啦
围观群众:9
更新于

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

二者区别:

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

例如:select current_timestamp from pub_employee结果如下:

PG数据库的to_date和to_timestamp 将字符串转换为时间格式

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当前的时间 大两个小时,但是结果如下:

PG数据库的to_date和to_timestamp 将字符串转换为时间格式

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

PG数据库的to_date和to_timestamp 将字符串转换为时间格式

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

PG数据库的to_date和to_timestamp 将字符串转换为时间格式

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

PG数据库的to_date和to_timestamp 将字符串转换为时间格式

============================================================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 返回的都是以下结果:

PG数据库的to_date和to_timestamp 将字符串转换为时间格式

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

PG数据库的to_date和to_timestamp 将字符串转换为时间格式

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

PG数据库的to_date和to_timestamp 将字符串转换为时间格式

非特殊说明,本文版权归 金科常识网 所有,转载请注明出处.

本文分类: 历史

本文标题: todate日期格式写法(PG数据库的todate和totimestamp 将字符串转换为时间格式)

本文网址: http://sddljzx.com/lishi/5782.html

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。