zoukankan      html  css  js  c++  java
  • PostgreSQL中的 时间格式转化常识

    下面的SQL文查询结果是 "2018-08-20 10:09:10.815125",并且返回类型可以当String处理。返回json等都方便使用。

    SQL> SELECT to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS');

    更新时,参数传入“2018-08-20 10:09:10.815125”的字符串,那么需要在SQL中转化来匹配updateTime字段的timeStamp数据类型。

    SQL> update tbl_A set username='XXX' where userid='001' and updateTime = to_timestamp('2018-08-20 10:09:10.815125','yyyy-mm-dd hh24:mi:ss.us');

    另附表一张

    函数 返回类型 描述 例子
    to_char(timestamp, text) text 把时间戳转换成字串 to_char(current_timestamp, 'HH12:MI:SS')
    to_char(interval, text) text 把时间间隔转为字串 to_char(interval '15h 2m 12s', 'HH24:MI:SS')
    to_char(int, text) text 把整数转换成字串 to_char(125, '999')
    to_char(double precision, text) text 把实数/双精度数转换成字串 to_char(125.8::real, '999D9')
    to_char(numeric, text) text 把numeric转换成字串 to_char(-125.8, '999D99S')
    to_date(text, text) date 把字串转换成日期 to_date('05 Dec 2000', 'DD Mon YYYY')
    to_timestamp(text, text) timestamp 把字串转换成时间戳 to_timestamp('05 Dec 2000', 'DD Mon YYYY')
    to_timestamp(double) timestamp 把UNIX纪元转换成时间戳 to_timestamp(200120400)
    to_number(text, text) numeric 把字串转换成numeric to_number('12,454.8-', '99G999D9S')
    模式 描述
    HH 一天的小时数(01-12)
    HH12 一天的小时数(01-12)
    HH24 一天的小时数(00-23)
    MI 分钟(00-59)
    SS 秒(00-59)
    MS 毫秒(000-999)
    US 微秒(000000-999999)
    AM 正午标识(大写)
    Y,YYY 带逗号的年(4和更多位)
    YYYY 年(4和更多位)
    YYY 年的后三位
    YY 年的后两位
    Y 年的最后一位
    MONTH 全长大写月份名(空白填充为9字符)
    Month 全长混合大小写月份名(空白填充为9字符)
    month 全长小写月份名(空白填充为9字符)
    MON 大写缩写月份名(3字符)
    Mon 缩写混合大小写月份名(3字符)
    mon 小写缩写月份名(3字符)
    MM 月份号(01-12)
    DAY 全长大写日期名(空白填充为9字符)
    Day 全长混合大小写日期名(空白填充为9字符)
    day 全长小写日期名(空白填充为9字符)
    DY 缩写大写日期名(3字符)
    Dy 缩写混合大小写日期名(3字符)
    dy 缩写小写日期名(3字符)
    DDD 一年里的日子(001-366)
    DD 一个月里的日子(01-31)
    D 一周里的日子(1-7;周日是1)
    W 一个月里的周数(1-5)(第一周从该月第一天开始)
    WW 一年里的周数(1-53)(第一周从该年的第一天开始)

    计算相差天数

    select date_part('day', '2015-01-15 17:05'::timestamp - '2013-01-14 16:05'::timestamp);

    在PostgreSQL中可以直接对时间进行加减运算:、

    SELECT now()::timestamp + '1 year';  --当前时间加1年
    SELECT now()::timestamp + '1 month';  --当前时间加一个月
    SELECT now()::timestamp + '1 day';  --当前时间加一天
    SELECT now()::timestamp + '1 hour';  --当前时间加一个小时
    SELECT now()::timestamp + '1 min';  --当前时间加一分钟
    SELECT now()::timestamp + '1 sec';  --加一秒钟
    select now()::timestamp + '1 year 1 month 1 day 1 hour 1 min 1 sec';  --加1年1月1天1时1分1秒
    SELECT now()::timestamp + (col || ' day')::interval FROM table --把col字段转换成天 然后相加
  • 相关阅读:
    angular二级联动菜单
    angular 实现自定义样式下拉菜单
    编程思想之回调
    编程思想之递归
    编程思想之迭代器
    阅读源码FluentScheduler
    改变spring-servlet.xml名字和默认位置
    thinkpad X1c 2018 插上电源风扇转速过快的解决办法
    python 和python-m 的区别
    电脑
  • 原文地址:https://www.cnblogs.com/xiaoliu66007/p/11015007.html
Copyright © 2011-2022 走看看