需要将UNIX转换为日期,但是某些值已经以日期格式进行

  • 本文关键字:日期 格式 UNIX 转换 sql postgresql
  • 更新时间 :
  • 英文 :


i有一个字段,该字段被推到我的数据库中,其中90%的值是毫秒 - unix时间戳,但大约5%的值是无效的,而5%是实际日期邮票,但该字段是文本字段。

我正在尝试使用以下方式转换日期:

cast(to_timestamp(cast(date_field as bigint)/1000) as date)

问题在于,已经是时间戳的日期字段(以文本字段格式(使我搞砸了,因为它们无法转换为bigint。这样的所有价值都以" 2016-"开始,因此我进行了一个查询以排除它们,但它们仍然存在。

排除这些的查询:

Select distinct
date
from table1
where date<>'2016%'

结果值之一:

2016-11-04 02:23:10

,因此您可以像日期一样转换时间:

SELECT FROM_UNIXTIME(t.date/1000,'%Y-%m-%d %H:%i:%s') FROM table1 t WHERE t.date NOT LIKE '2016-%' AND t.date IS NOT NULL;

你可以做类似:

where date like '20%' --for years after 2000 (there isnt yet a unix timestimp that starts with 2 )
or date like '19%' (there isnt yet a unix timestimp that starts with 19 )

最新更新