如果仅按日和月的日期相同,则使用 Oracle SQL 比较两个日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21830932/
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-09-19 02:14:27  来源:igfitidea点击:

Compare two dates with Oracle SQL if it's same date by only day and month

sqloracledatecompare

提问by Christian

I have saved a date in my oracle db. (Pattern: dd.MM.yyyy) Now I have to check/compare by day and month, if today ss the same date or exactly 6 months after. But the year has to be ignored. For example. My date is 01.02.2001. Then it has to be "true" at the 01.02.2002.

我在我的 oracle 数据库中保存了一个日期。(模式:dd.MM.yyyy)现在我必须按日和月检查/比较,如果今天是同一日期或正好是 6 个月之后。但是年份必须被忽略。例如。我的日期是 01.02.2001。那么它必须在 01.02.2002 为“真”。

Also I need to check if the date + 6 months is true. (halfyearly). (For example 01.02.2002 --> 01.08.2002)

我还需要检查日期 + 6 个月是否正确。(每半年)。(例如 01.02.2002 --> 01.08.2002)

I tried with sysdate, but I'm a noob in sql.

我尝试使用 sysdate,但我是 sql 的菜鸟。

回答by Gordon Linoff

You can do the comparison using to_char():

您可以使用to_char()以下方法进行比较:

where to_char(yourdate, 'MM-DD') = to_char(sysdate, 'MM-DD')

For six months:

六个月:

where to_char(yourdate, 'MM-DD') = to_char(add_months(sysdate, 6), 'MM-DD')

回答by MR AND

You can also use

你也可以使用

substr(date1,5) =substr(date2,5)

this code compares first five characters of the data and checks if they are identical or not.

此代码比较数据的前五个字符并检查它们是否相同。