欢迎投稿

今日深度:

对postgresql日期和时间的比较,

对postgresql日期和时间的比较,


目录
  • postgresql日期和时间比较
    • DB里保存到时分秒,需要和年月日比较
    • db里存储date或者timestamp字段
  • 总结

    postgresql日期和时间比较

    DB里保存到时分秒,需要和年月日比较

    select date_trunc('day',now())=date_trunc('day',date('20200615'))			--true
    select date_trunc('day',date('20200611'))					--2020-06-11 00:00:00+00
    select * from users where date_trunc('day',birthday)=date_trunc('day',date('20200401'))

    db里存储date或者timestamp字段

    需要和字符串比较时,建议先使用to_date或者to_timestamp转换。

    测试发现pgsql往类型为timestamp的列插入字符串数据,或者用date/timestamp类型的数据跟字符串数据作比较时,会自动转换成对应的date/timestamp。

    oracle未测试。

    select to_date('2019-01-15 18:33:41','yyyy-MM-dd hh24:mi:ss');
    select to_timestamp('2019-01-15 18:33:41','yyyy-MM-dd hh24:mi:ss');
    select to_date('2019-01-15 18:33:42','yyyy-MM-dd hh24:mi:ss')= to_timestamp('2019-01-15 00:00:00','yyyy-MM-dd hh24:mi:ss');
    >>true
    select to_timestamp('2019-01-15 18:33:42','yyyy-MM-dd hh24:mi:ss')- to_date('2019-01-15 18:33:42','yyyy-MM-dd hh24:mi:ss');
    >>"18:33:42"
    select to_timestamp('2019-01-15 18:33:42','yyyy-MM-dd hh24:mi:ss')='2019/01/15';
    >>false
    
    select to_date('2019-01-15 18:33:42','yyyy-MM-dd hh24:mi:ss')='2019/01/15';
    >>true
    select to_date('2019-01-15 18:33:42','yyyy-MM-dd hh24:mi:ss')='2019-01-15';
    >>true
    select to_date('2019-01-15 18:33:42','yyyy-MM-dd hh24:mi:ss')='20190115';
    >>true
    select to_date('2019-01-15 18:33:42','yyyy-MM-dd hh24:mi:ss')='2019/01-15';
    >>ERROR:  date型の入力構文が不正です: "2019/01-15"
    
    
    SELECT 
    	time,
    	to_timestamp('2011-12-13 14:15:16','yyyy-MM-dd hh24:mi:ss'),
    	time=to_timestamp('2011-12-13 14:15:16','yyyy-MM-dd hh24:mi:ss'),
    	time,to_date('2011-12-13 14:15:16','yyyy-MM-dd hh24:mi:ss'),
    	time=to_date('2011-12-13 14:15:16','yyyy-MM-dd hh24:mi:ss')
    FROM public.product where id =21;
    >>"2011-12-13 14:15:16+09"
    >>"2011-12-13 14:15:16+09"
    >>true
    >>"2011-12-13 14:15:16+09"
    >>"2011-12-13"
    >>false
    

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持PHP之友。

    您可能感兴趣的文章:
    • PostgreSQL中查看当前时间和日期的几种常用方法
    • PostgreSQL设置时区、时间/日期函数汇总大全
    • PostgreSQL的日期时间差DATEDIFF实例详解
    • PostgreSQL时间日期的语法及注意事项
    • PostgreSQL中的日期/时间函数详解

    www.htsjk.Com true http://www.htsjk.com/shujukunews/48528.html NewsArticle 对postgresql日期和时间的比较, 目录 postgresql日期和时间比较 DB里保存到时分秒,需要和年月日比较 db里存储date或者timestamp字段 总结 postgresql日期和时间比较 DB里保存到时分秒,需要和...
    评论暂时关闭