What's exactly operator to compare between `bigint` and `date` in postgresql?

独自空忆成欢 提交于 2019-12-11 08:38:21

问题


My story evaluated from this:

I created a function create dynamic partition table with Table_Year_Month format such as table_2018_04, table_2018_05 .... The arguments of creation partition function are bigint such as create_partition_function(1518164237,1520583437) ;. After that I convert bigint to date to can get year and month from timestamp. But the check function ( check(timestamp >= date)) don't work

I can't compare bigint >= date in sql. What's operator can compare their?

I tried convert timestamp to datetime with UNIX_TIMESTAMP function of sql but don't work

CREATE OR REPLACE FUNCTION create_partition_function( DATE, DATE )
returns void AS $$
DECLARE
    create_query text;
    index_query text;
BEGIN
    FOR create_query, index_query IN SELECT
            'create table user_event_firebase_'
            || TO_CHAR( d, 'YYYY_MM' )
            || ' ( check( timestamp >= UNIX_TIMESTAMP(date,'%Y %M %D' )'''
            || TO_CHAR( d, 'YYYY-MM-DD' )
            || ''' and timestamp < UNIX_TIMESTAMP(date,'%Y %M %D' ) '''
            || TO_CHAR( d + INTERVAL '1 month', 'YYYY-MM-DD' )
            || ''' ) ) inherits ( user_event_firebase );',
            'create index user_event_firebase_'
            || TO_CHAR( d, 'YYYY_MM' )
            || '_time on user_event_firebase_' 
            || TO_CHAR( d, 'YYYY_MM' )
            || ' ( timestamp );'
        FROM generate_series( to_timestamp($1), to_timestamp($2), '1 month'::interval ) AS d
    LOOP
        EXECUTE create_query;
        EXECUTE index_query;
    END LOOP;
END;
$$
language plpgsql;

p/s : bigint and dateare data type in sql.

ERROR:  invalid input syntax for integer: "2018-02-09"
CONTEXT:  SQL statement "create table user_event_firebase_2018_02 ( check( timestamp >= bigint '2018-02-09' and timestamp < bigint '2018-03-09' ) ) inherits ( user_event_firebase );"
PL/pgSQL function create_partition_function(date,date) line 21 at EXECUTE

回答1:


You can convert date/timestamp to seconds using

SELECT extract(epoch from '2016-05-03'::date)
--result: 1462233600

SELECT to_timestamp(1462233600)::date;
--result: '2016-05-03'


来源:https://stackoverflow.com/questions/49751570/whats-exactly-operator-to-compare-between-bigint-and-date-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!