Postgres to_timestamp() 无法转换 unix epoch 字符串 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
flgn88

Postgres to_timestamp() 无法转换 unix epoch 字符串

  •  
  •   flgn88 2020 年 10 月 4 日 2816 次点击
    这是一个创建于 2029 天前的主题,其中的信息可能已经有所发展或是发生改变。

    表结构如下:

    create table item ( "ID" varchar not null, "unixTime" varchar not null, ); 

    现在

    SELECT TO_TIMESTAMP("unixTime"); 

    就总是提示
    ERROR: function to_timestamp(character varying) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.

    但随手试了几个值都没啥问题,比如:
    select to_timestamp('1358121600');
    select to_timestamp('969408000');

    初步怀疑表里有不同 format 的 timestamp 的值,然后 to_timestamp()不能根据不同的 format 自动智能转换?
    有没有大佬可以启发下,看这样的情况怎么解决,急急急~谢谢谢谢

    hooopo
        1
    hooopo  
       2020 年 10 月 4 日 via Android
    SELECT TO_TIMESTAMP(unixTime);
    flgn88
        2
    flgn88  
    OP
       2020 年 10 月 4 日
    @hooopo 这个不是问题啦,postgres 里字段名有大写的话要加双引号的~
    hooopo
        3
    hooopo  
       2020 年 10 月 4 日 via Android
    很简单啊 先用 limit 1 测试下是不是数据问题 数据问题就修数据
    flgn88
        4
    flgn88  
    OP
       2020 年 10 月 4 日
    @hooopo Limit 1 也报错,而且 80M 条数据,没法测呀,我倒是想有个函数可以把这些 invalid 的数据排除出去。。。
    hooopo
        5
    hooopo  
       2020 年 10 月 4 日 via Android
    一个正则就行啊
    MoYi123
        6
    MoYi123  
       2020 年 10 月 4 日   1
    select TO_TIMESTAMP("unixTime"::int) from item;
    flgn88
        7
    flgn88  
    OP
       2020 年 10 月 4 日
    @MoYi123 完美!!感谢感谢老哥
    sagaxu
        8
    sagaxu  
       2020 年 10 月 4 日 via Android
    不能查一下手册看看这个函数的定义?
    sfqtsh
        9
    sfqtsh  
       2020 年 10 月 4 日 via Android
    \df to_timestamp

    你看有 character varying 参数的吗?

    显然没有!
    flgn88
        10
    flgn88  
    OP
       2020 年 10 月 4 日 via iPhone
    @sfqtsh 有支持 string 啊
    sfqtsh
        11
    sfqtsh  
       2020 年 10 月 4 日 via Android   1
    @nuistzhou 那是 format function 一种,且有两个参数。

    to_timestamp ( text, text ) → timestamp with time zone

    Converts string to time stamp according to the given format. (See also to_timestamp(double precision) in Table 9.32.)

    to_timestamp('05 Dec 2000', 'DD Mon YYYY') → 2000-12-05 00:00:00-05
    flgn88
        12
    flgn88  
    OP
       2020 年 10 月 4 日
    @sfqtsh 哦,你说的对。
    我需要的其实是这个:
    ```
    to_timestamp ( double precision ) → timestamp with time zone

    Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp with time zone

    to_timestamp(1284352323) → 2010-09-13 04:32:03+00
    ```
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2668 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 62ms UTC 10:18 PVG 18:18 LAX 03:18 JFK 06:18
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86