天天看点

SQL:pgsql中查询一段时间内数据及相关时间操作

需求 : 查询某个时间一秒内的数据

解决 :将时间转换为时间戳,加一秒

SELECT
  * 
FROM
  buct_composedata 
where paraid = '14112402C30000000401' and systemtime between ('2020-05-31 22:01:30') and ('2020-05-31 22:01:30'::timestamp + '1 sec')      
SQL:pgsql中查询一段时间内数据及相关时间操作
函数 返回类型 描述 例子
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', now() - '1997-02-08 '::timestamp);      

计算固定间隔时间

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字段转换成天 然后相加