Oracle SQL - counting a continuous number of weeks between two dates

I need to count a continuous number of weeks between two dates, at the moment I can partially do this using

select
(next_day(TO_DATE('01-01-1994', 'DD/MM/YYYY'), 'MONDAY')-next_day(TO_DATE('01-01-1995',
 'DD/MM/YYYY'), 'MONDAY'))/7 countinous_weeks
from dual;

I get 52 weeks between the two dates, but when I check the last week of '01 -01-1994 ', I get 53 weeks.

select 
to_char(TO_DATE('31-12-1994', 'DD/MM/YYYY'),'ww') as last_week_test 
from dual; 

It’s obvious enough to understand why this is happening, but I would appreciate it if someone could come up with a way to count the number of weeks continuously without using next_day, because that is where I think the problem is.

Thanks so much for any advice in advance.

+4
source share
2 answers

You can use the function truncper day as follows:

select (trunc(TO_DATE('01-01-1994', 'DD/MM/YYYY'),'d')-trunc(TO_DATE('01-01-1995',
 'DD/MM/YYYY'),'d'))/7  countinous_weeks from dual;

next_day, . , .

+1

, , , . , ?

, , , , 1?

.

select countinous_weeks, decode(countinous_weeks-52,0,trunc(countinous_weeks),trunc(countinous_weeks)+1)
from (
select (TO_DATE('01-01-1995', 'DD/MM/YYYY') - TO_DATE('01-01-1994','DD/MM/YYYY'))/7 countinous_weeks
from dual) wks
0

Source: https://habr.com/ru/post/1534809/


All Articles