1

I need to print the last sunday of the year from which adding 7 to it will give me all the sundays of next year.

I have the code to print all sundays for a particular year if i have a start date but i need the user to put the year so that last sunday of the previous year will be generated and 7 will be added to get first sunday of that year and so on till it reaches last sunday of next year

For example input year is 2017 it will check the last sunday of 2016 and add 7 to it to get first sunday of 2017 which is 1-1-2017 and it will go on printing all sundays till it reaches 31st december 2017

Abhijith
  • 77
  • 2
  • 14
  • 1
    Can be done. But please share what you have done so far – Rogue Coder Feb 02 '17 at 05:25
  • If you add 7 to the **last** Sunday of the year, how do you get all the Sundays of **that** year? Do you mean of the **next** year? Or do you mean you need the **first** Sunday of the year? –  Feb 02 '17 at 05:38
  • Hi I havent done yet .. Its just that i need to accept a year from user.. for example..... enter year:2014 output: 1. Last sunday of 2013 2.First sunday of 2014 3.Second sunday of 2014 4.third sunday of 2014. etc etc etc – Abhijith Feb 02 '17 at 07:01
  • @mathguy sorry all sundays of next year like i said if i input 2016 the last sunday of 2015 and by adding 7 to it i shall get first sunday of 2016.. M not able to generate code for it – Abhijith Feb 02 '17 at 07:05
  • @RogueCoder plz help – Abhijith Feb 02 '17 at 09:05
  • I see - you need to take an input, and create the "24 Dec." of the year prior to the input? And you don't know how to do that? I'll edit my answer to show how. You said you **do** know how to generate all the Sundays of the given year and you only need the last Sunday of the prior year; I will still not include the part you already know how to do. –  Feb 02 '17 at 13:51
  • @mathguy well I have the code to print all sundays of a particular year only if I have a start date for example if i have to print all sundays of 2017 then manually i know 25th dec was last sunday for 2016 so i hardcoded it and added 7 to it which gave me all sundays of 2017.so instead of that hardcoding i need to generate that sunday because not everytime 25th dec would be sunday. iam cluess how to get that last sunday of a year and like i said if i have to find all sundays of 2017 then i need the last sunday of 2016 from which by adding 7 i shall get 2017's first sunday and rest sundays – Abhijith Feb 03 '17 at 04:54
  • I don't understand - did you try the answer I posted a day ago? Did it not work? –  Feb 03 '17 at 05:00
  • @mathguy no brother it didnt – Abhijith Feb 03 '17 at 06:27
  • issue is solved – Abhijith Feb 03 '17 at 09:58
  • So, what was the problem? Did you need to change what I provided? Others who will visit this thread in the future looking for answers to their problems may need to know. –  Feb 03 '17 at 13:26
  • Possible duplicate of [Get list of all tables in Oracle?](http://stackoverflow.com/questions/205736/get-list-of-all-tables-in-oracle) – Abhijith Feb 08 '17 at 07:08

1 Answers1

3

The function next_day() takes two arguments: a date and the name of a day of the week. It returns the closest "next" day (following the date argument) that matches the given day of the week. So the result is between one and seven days forward. (If you want 'Tuesday' and the input date is a Tuesday, the function returns the date seven days later.)

If you want the last Sunday of a year, it will be between Dec. 25 and Dec. 31. So if you call the next_day() function with the arguments Dec. 24 (!!) and 'Sunday' you'll get what you want.

The result will have the same time-of-day as the date argument, so if you give a date without a time-of-day, so will be the output (which is probably what you want). So:

select next_day(date '2016-12-24', 'Sunday') from dual;

NEXT_DAY(D
----------
2016-12-25

Added: If you take an input from your user, as a bind variable, you can do something like this:

select next_day(to_date(:input_year - 1 || '-12-24', 'yyyy-mm-dd'), 'Sunday') from dual;

If you provide 2017 as input (whatever mechanism your interface has for bind variables), the output will be 2016-12-25 (in DATE data type, so don't ask "in what format" - dates don't have a format!)

  • whats the use of '-12-24' please explain as i said i need the last sunday of a year, if i put 2017 as input then 2016s last sunday should be printed which is 25th december 2016 – Abhijith Feb 03 '17 at 05:57
  • If you input the year 2017 then `:input_year - 1` is 2016. `||` is the concatenate operator (add more text). The extra text is `12-24` (a **fixed** string: I always need exactly the date December 24 for the computation). So the string becomes `'2016-12-24'` which is made into a date by `to_date()`. So if you give the year 2017, this creates the date `2016-12-24` automatically, and it uses it in the computation of the last Sunday date in 2016, as explained in the earlier part of the answer. –  Feb 03 '17 at 13:29