lightdb上我们分别可以用下面的函数对时间进行加法和减法运算
方法一:
select hiredate - interval '5 day' as hd_minus_5D,
hiredate + interval '5 day' as hd_plus_5D,
hiredate - interval '5 month' as hd_minus_5M,
hiredate + interval '5 month' as hd_plus_5M,
hiredate - interval '5 year' as hd_minus_5Y,
hiredate + interval '5 year' as hd_plus_5Y
from emp;

方式二:这个与Oracle的方式一样
select hiredate - 5 as hd_minus_5D,
hiredate + 5 as hd_plus_5D,
add_months(hiredate, -5) as hd_minus_5M,
add_months(hiredate, 5) as hd_plus_5M,
add_months(hiredate, -5 * 12) as hd_minus_5Y,
add_months(hiredate, 5 * 12) as hd_plus_5Y
from emp;

两种方法分别返回timestamp without time zone和date类型
create table t1 as
select hiredate - interval '5 day' as hd_minus_5D,
hiredate + interval '5 day' as hd_plus_5D,
hiredate - interval '5 month' as hd_minus_5M,
hiredate + interval '5 month' as hd_plus_5M,
hiredate - interval '5 year' as hd_minus_5Y,
hiredate + interval '5 year' as hd_plus_5Y
from emp;
create table t2 as
select hiredate - 5 as hd_minus_5D,
hiredate + 5 as hd_plus_5D,
add_months(hiredate, -5) as hd_minus_5M,
add_months(hiredate, 5) as hd_plus_5M,
add_months(hiredate, -5 * 12) as hd_minus_5Y,
add_months(hiredate, 5 * 12) as hd_plus_5Y
from emp;
lightdb@postgres=# \d t1
Table "public.t1"
Column | Type | Collation | Nullable | Default
-------------+-----------------------------+-----------+----------+---------
hd_minus_5d | timestamp without time zone | | |
hd_plus_5d | timestamp without time zone | | |
hd_minus_5m | timestamp without time zone | | |
hd_plus_5m | timestamp without time zone | | |
hd_minus_5y | timestamp without time zone | | |
hd_plus_5y | timestamp without time zone | | |
lightdb@postgres=# \d t2
Table "public.t2"
Column | Type | Collation | Nullable | Default
-------------+------+-----------+----------+---------
hd_minus_5d | date | | |
hd_plus_5d | date | | |
hd_minus_5m | date | | |
hd_plus_5m | date | | |
hd_minus_5y | date | | |
hd_plus_5y | date | | |
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




