Difference in days between 2 dates Oracle SQL

后端 未结 2 1203
我寻月下人不归
我寻月下人不归 2021-01-28 21:04

Alright, so I\'ve checked many many many other posts on stackoverflow to see if this is mentioned anywhere, and the answers provided don\'t quite make sense to me...I\'m thinkin

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-28 22:08

    Assuming that purch_date is defined as a DATE

    SELECT purch_id,
           (trunc(sysdate, 'YYYY') +
             interval '11' month +
             interval '24' day) -
            purch_date days_to_christmas
      FROM CS260USER.candy_purchase;
    

    A couple of notes

    1. If purch_date has a time component, you might want to truncate the result of the date subtraction.
    2. If this query is executed between December 26 and December 31, you'll still be looking at this year's Chrismas (i.e. a date in the past) rather than next year's Christmas. I would thing that if purch_date was, for example, December 30, that you would want the result to be 360 days until the next Christmas rather than -5 days until the last Christmas.

提交回复
热议问题