postgreSQL 使用timestamp转成date格式
作者:abce
这篇文章主要介绍了postgreSQL 使用timestamp转成date格式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
尝试了以下两种方式,将pg中的timestamp格式转换成date格式:
select to_date( to_char( f.begin_time, 'yyyy-mm-dd' ), 'yyyy-mm-dd' ) from hafd f
select f.begin_time::DATE from hafd f
大概比较了一下,9万条测试数据,方式二的性能更好!
补充: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');
另附表一张
to_char(timestamp, text)
把时间戳转换成字串
to_char(current_timestamp, 'HH12:MI:SS')
to_char(interval, text)
把时间间隔转为字串
to_char(interval '15h 2m 12s', 'HH24:MI:SS')
to_char(int, text)
把整数转换成字串
to_char(125, '999')
to_char(double precision, text)
把实数/双精度数转换成字串
to_char(125.8::real, '999D9')
to_char(numeric, text)
把numeric转换成字串
to_char(-125.8, '999D99S')
to_date(text, text)
把字串转换成日期
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')
PostgreSQL HOT与PHOT有哪些区别
2022-09-09
PostgreSQL事务回卷实战案例详析
2022-03-03
Postgresql数据库角色创建登录详解
2023-02-02
PostgreSQL limit的神奇作用详解
2022-09-09
Postgresql之时间戳long,TimeStamp,Date,Stri
2022-09-09
postgresql踩坑系列之关于to_date()问题
2022-09-09
postgresql中的时间戳格式化
2022-09-09
关于postgresql timestamp时间戳问题
2022-09-09
美国设下计谋,用娘炮文化重塑日本,已影响至中国
2021-11-19
时空伴随者是什么意思?时空伴随者介绍
2021-11-09
工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
2021-11-05
2022年放假安排出炉:五一连休5天 2022年所有节日一览表
2021-10-26
电脑版
-
返回首页
2006-2023 脚本之家 JB51.Net , All Rights Reserved.
苏ICP备14036222号