如何使用 postgresql/netezza 从日期时间中减去天数或月数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27283951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:42:13  来源:igfitidea点击:

how to subtract days or months from datetime using postgresql/netezza

postgresqlnetezza

提问by moe

I have this sql query that is working fine but would like to convert it into some kind of postgresql or netezza equivalent query. Here are my queries:

我有这个运行良好的 sql 查询,但想将其转换为某种 postgresql 或 netezza 等效查询。以下是我的疑问:

SELECT name, location, Date FROM myTable WHERE [Date] < DateAdd(hh, 48, [shipDate]

and other query

和其他查询

SELECT name, location, Date FROM myTable WHERE [Date] < DateAdd(d, 90, [shipDate]

采纳答案by ScottMcG

For Netezza the easiest thing to do is use an interval calculation:

对于 Netezza,最简单的方法是使用间隔计算:

SELECT name, location, Date FROM myTable WHERE Date < shipDate + interval '48 hours';
SELECT name, location, Date FROM myTable WHERE Date < shipDate + interval '2 days';
SELECT name, location, Date FROM myTable WHERE Date < shipDate + interval '90 days';

You can also use add_months if you want to do month calculations that are aware of the different length of the months.

如果要进行了解月份不同长度的月份计算,也可以使用 add_months。

SELECT name, location, Date FROM myTable WHERE Date < add_months(shipdate, 3);