动态(基于列)间隔
如何向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我通常将数字乘以
interval '1 day'
或类似值,例如:I usually multiply the number by
interval '1 day'
or similar, e.g.:我知道这是一年前的事了,但是如果您需要使用列来指定实际间隔(例如“天”、“月”),那么值得知道的是您也可以将字符串转换为间隔,给出
:原来的问题将变成:
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:
So the the original question would become:
我更喜欢这种方式。我认为它非常简单和干净。
在 Postgres 中,您需要
interval
才能将+
运算符与timestamp
一起使用,其中您可以使用秒、分钟、天、月...
您可以将数字替换到您的列中。
I prefer this way. I think its pretty easy and clean.
In Postgres you need
interval
to use+
operator withtimestamp
where you can use seconds, minutes, days, months...
and you can replace the numbers to your column.
使用
make_interval()
但一般来说,使用定义为
interval
的列可能是一个更好的主意,然后您可以在其中存储值时使用任何您想要的单位。Use
make_interval()
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.要创建基于列值的间隔,我建议在表中添加两列。例如,列“period_value”::INT4 和列“period_name”::VARCHAR。
“period_name”可以存储以下值:
你可以写:
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:
Now you can write:
如果我们有间隔字符串值的字段,例如“41 年 11 个月 4 天”,并且想要将其转换为出生日期,请使用以下查询:
dob 是要转换'41 年的日期字段例如“11 个月 4 天” 到“1972/10/14”
patent_age 是 varchar 字段,其中包含“41 年 11 个月 4 天”等字符串
,这是将年龄转换回出生日期的查询
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 :
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
基于列 ID 进行更新是为我创建一些随机测试数据的有用方法。
Updating based on a column ID was a useful way to create some randomised test data for me.