目录
- postgresql时间戳格式化
- postgresql中时间戳格式转化常识
- 另附表一张
- 总结
postgresql时间戳格式化
平时总记不住pg里面怎么将时间戳格式化,记录常用的格式化时间
— 年
select to_char(to_timestamp(1608018517000 / 1000),\’yyyy\’);
— 年-月
select to_char(to_timestamp(1608018517000 / 1000),\’yyyy-MM\’);
— 年-月-日
select to_char(to_timestamp(1608018517000 / 1000),\’yyyy-MM-dd\’);
— 年-月-日 时
select to_char(to_timestamp(1608018517000 / 1000),\’yyyy-MM-dd HH24\’); 对应JAVA 的“yyyy-MM-dd HH”
— 年-月-日 时:分
select to_char(to_timestamp(1608018517000 / 1000),\’yyyy-MM-dd HH24:MI\’);对应JAVA 的“yyyy-MM-dd HH:mm”
— 年-月-日 时:分:秒
select to_char(to_timestamp(1608018517000 / 1000),\’yyyy-MM-dd HH24:MI:SS\’);对应JAVA 的“yyyy-MM-dd HH:mm:ss”
postgresql中时间戳格式转化常识
前提:当数据库中保存的是timestamp类型时,我们需要通过这个时间戳来做乐观数据锁,那么久需要Select出来,然后在更新的时候在Update的where条件中判断时间戳是否与查询时相同。
下面的SQL文查询结果是 "2018-08-20 10:09:10.815125",并且返回类型可以当String处理。返回json等都方便使用。
SQL> select to_char(updateTime, \’yyyy-mm-dd hh24:mi:ss.us\’) from tbl_A;
更新时,参数传入“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\’);
另附表一张
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持悠久资源。