SQL SQL中当前日期两周后的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16135261/
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
date after two weeks from current date in SQL
提问by mpluse
How can I do to extract date after two weeks from current date?
从当前日期起两周后如何提取日期?
Example
例子
SELECT blabla
FROM table
WHERE blabla IN [sysdate + 2 weeks]
回答by Trinimon
How about ... ?
怎么样 ... ?
SELECT blabla
FROM table
WHERE blabla > SYSDATE AND blabla < SYSDATE +14;
This SYSDATE +x
means x
days from now on.
这SYSDATE +x
意味着x
从现在开始的几天。
p.s.: for records with date now +14use ...
ps:对于现在日期为 +14 的记录,请使用 ...
SELECT blabla
FROM table
WHERE blabla LIKE SYSDATE +14;
p.p.s.as Ben pointed out below, there is a potential risk in using LIKE
as it depends on the nls_date_format
parameter. Take care that an appropriate format is set or have a look at Gordons approach. @Ben: Thanks for the note.
正如 Ben 在下面指出的那样,pps存在潜在风险,LIKE
因为它取决于nls_date_format
参数。注意设置适当的格式或查看 Gordons 方法。@Ben:感谢您的注意。
Hope this helps ... Cheers!
希望这有助于...干杯!
回答by Gordon Linoff
When you are doing this, you need to be careful about the time fractions. If you are not concerned about indexes, then you can do:
当你这样做时,你需要注意时间分数。如果你不关心索引,那么你可以这样做:
where trunc(blabla) = trunc(sysdate) + 14
However, this can preclude the use of an index, because of the function. One way around this is:
但是,由于功能的原因,这可能会阻止使用索引。解决这个问题的一种方法是:
where blabla >= trunc(sysdate) + 14 and blabla < trunc(sysdate) + 15
回答by Brian
To be more standards compliant and avoid type conversions use CURRENT_DATE to get the date and INTERVAL to add a time interval to it.
为了更符合标准并避免类型转换,请使用 CURRENT_DATE 获取日期并使用 INTERVAL 为其添加时间间隔。
SELECT blabla
FROM table
WHERE blabla = CURRENT_DATE + INTERVAL '14' DAY