动态(基于列)间隔

发布于 2024-10-21 12:11:50 字数 177 浏览 1 评论 0原文

如何向 NOW 添加动态(基于列)天数?

SELECT NOW() + INTERVAL a.number_of_days "DAYS" AS "The Future Date" 
FROM a;

其中 a.number_of_days 是整数?

How do I add a dynamic (column based) number of days to NOW?

SELECT NOW() + INTERVAL a.number_of_days "DAYS" AS "The Future Date" 
FROM a;

Where a.number_of_days is an integer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

源来凯始玺欢你 2024-10-28 12:11:50

我通常将数字乘以 interval '1 day' 或类似值,例如:

select now() + interval '1 day' * a.number_of_days from a;

I usually multiply the number by interval '1 day' or similar, e.g.:

select now() + interval '1 day' * a.number_of_days from a;
好久不见√ 2024-10-28 12:11:50

我知道这是一年前的事了,但是如果您需要使用列来指定实际间隔(例如“天”、“月”),那么值得知道的是您也可以将字符串转换为间隔,给出

SELECT now()+ CAST(the_duration||' '||the_interval AS Interval)

:原来的问题将变成:

SELECT now() + CAST(a.number_of_days||" DAYS" AS Interval) as "The Future Date" FROM a;

I know this is a year old, but if you need to use a column to specify the actual interval (e.g. 'days', 'months', then it is worth knowing that you can also CAST your string to an Interval, giving:

SELECT now()+ CAST(the_duration||' '||the_interval AS Interval)

So the the original question would become:

SELECT now() + CAST(a.number_of_days||" DAYS" AS Interval) as "The Future Date" FROM a;
緦唸λ蓇 2024-10-28 12:11:50

我更喜欢这种方式。我认为它非常简单和干净。
在 Postgres 中,您需要 interval 才能将 + 运算符与 timestamp 一起使用,

select (3||' seconds')::interval;

select now()+ (10||' seconds')::interval,now();

其中您可以使用秒、分钟、天、月...
您可以将数字替换到您的列中。

select now()+ (column_name||' seconds')::interval,now()
from your_table;

I prefer this way. I think its pretty easy and clean.
In Postgres you need interval to use + operator with timestamp

select (3||' seconds')::interval;

select now()+ (10||' seconds')::interval,now();

where you can use seconds, minutes, days, months...
and you can replace the numbers to your column.

select now()+ (column_name||' seconds')::interval,now()
from your_table;
终陌 2024-10-28 12:11:50

使用 make_interval()

SELECT NOW() + make_interval(days => a.number_of_days) AS "The Future Date" 
FROM a;

但一般来说,使用定义为 interval 的列可能是一个更好的主意,然后您可以在其中存储值时使用任何您想要的单位。

Use make_interval()

SELECT NOW() + make_interval(days => a.number_of_days) AS "The Future Date" 
FROM a;

But in general it might be a better idea to use a column defined as interval, then you can use any unit you want when you store a value in there.

七禾 2024-10-28 12:11:50

要创建基于列值的间隔,我建议在表中添加两列。例如,列“period_value”::INT4 和列“period_name”::VARCHAR。
“period_name”可以存储以下值:

  • 微秒
  • 毫秒 秒 分钟
  • 小时
  • 季度
  • 世纪
  • 十年
  • 千年
  • 现在
+--------------+-------------+
| period_value | period_name |
+--------------+-------------+
| 2            | minute      |
+--------------+-------------+

你可以写:

SELECT NOW() - (period_value::TEXT || ' ' || period_name::TEXT)::INTERVAL FROM table;

To creating intervals those based on column values, I recommend to add two columns in your table. For example, column "period_value"::INT4 and column "period_name"::VARCHAR.
Column "period_name" can store the following values:

  • microsecond
  • milliseconds
  • second
  • minute
  • hour
  • day
  • week
  • month
  • quarter
  • year
  • decade
  • century
  • millennium
+--------------+-------------+
| period_value | period_name |
+--------------+-------------+
| 2            | minute      |
+--------------+-------------+

Now you can write:

SELECT NOW() - (period_value::TEXT || ' ' || period_name::TEXT)::INTERVAL FROM table;
浮华 2024-10-28 12:11:50

如果我们有间隔字符串值的字段,例如“41 年 11 个月 4 天”,并且想要将其转换为出生日期,请使用以下查询:

UPDATE "february14" set dob = date '2014/02/01'  - (patient_age::INTERVAL) 

dob 是要转换'41 年的日期字段例如“11 个月 4 天”“1972/10/14”
patent_age 是 varchar 字段,其中包含“41 年 11 个月 4 天”等字符串

,这是将年龄转换回出生日期的查询

SELECT now() - INTERVAL '41 years 10 mons 10 days';

If we have field with interval string value such as '41 years 11 mons 4 days' and want to convert it to date of birth use this query :

UPDATE "february14" set dob = date '2014/02/01'  - (patient_age::INTERVAL) 

dob is date field to convert '41 years 11 mons 4 days' to '1972/10/14' for example
patient_age is varchar field that have string like '41 years 11 mons 4 days'

And this is query to convert age back to date of birth

SELECT now() - INTERVAL '41 years 10 mons 10 days';
梦里人 2024-10-28 12:11:50

基于列 ID 进行更新是为我创建一些随机测试数据的有用方法。

update study_histories set last_seen_at = now() - interval '3 minutes' * id;

Updating based on a column ID was a useful way to create some randomised test data for me.

update study_histories set last_seen_at = now() - interval '3 minutes' * id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文