开发者

Determining if a leap day falls between two days with DB2 SQL

I have a table with two dates, "Start_Date" and开发者_Python百科 "End_Date". In DB2 SQL, is there a way to determine if a leap day falls between these two dates?

Thank you!


Sure, you can do this using some date math and the DAYS function, by comparing the number of days between the the start and end date to the number of days between the start date and end date when they've both been shifted by 1 year.

If the number of days between the two dates is the same in both cases, then no leap day has occurred. If the number of days differs, then there has been at least 1 leap day.

This expression will return the number of leap days:

select
   ( DAYS(end_date + 1 year) - DAYS(start_date + 1 year) ) -
   ( DAYS(end_date)          - DAYS(start_date) )
from
   sysibm.sysdummy1

This should work as long as end_date >= start_date.

It's trivial to encapsulate this into a scalar User Defined Function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜