我在PostgreSQL中有一个字段: “带时区的时间戳比较”
我需要使用JS的数据范围比较法
var start = Date.UTC(2012,02,30);//1333065600000
var end = Date.UTC(2013,02,30); //1364601600000
导致bigint数字:
如何在sql PostgreSQL请求中使用start(1333065600000)和end(1364601600000)?
答案 0 :(得分:1)
尝试这样的事情:
select *
from my_table
where date_field between to_timestamp(1333065600000) and to_timestamp(1364601600000);
答案 1 :(得分:0)
这与TO_TIMESTAMP
基本相同,但如果服务器未以UTC运行,则转换为UTC时区。
SELECT * FROM table WHERE date BETWEEN
((TIMESTAMP WITH TIME ZONE 'epoch' + 1333065600 * INTERVAL '1 second') AT TIME ZONE 'UTC')
AND
((TIMESTAMP WITH TIME ZONE 'epoch' + 1364601600 * INTERVAL '1 second') AT TIME ZONE 'UTC');
请注意,Javascript epoch准确度是毫秒,而Unix纪元只有几秒。你必须从输入值中丢弃最后三位数(就像我在上面的例子中所做的那样),或者除以1000(例如下面的例子)。
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1333065600000/1000 * INTERVAL '1 second') AT TIME ZONE UTC;