Home > Software design >  how can I get next particular date from a given date in sql?
how can I get next particular date from a given date in sql?

Time:09-22

I want to find coming thursday date from a particular date. For example I want 23-09-2021(Thursday) from 22-09-2021 date. I want 30-09-2021 from 23-09-2021. Whatever date I put, I want coming thursday date.

CodePudding user response:

This is a question of adding the right number of days. Assuming you are using an American English language version of SQL Server, you can use a brute force method:

select dateadd(day,
               (case datename(weekday, datecol)
                   when 'Monday' then 3
                   when 'Tuesday' then 2
                   when 'Wednesday' then 1
                   when 'Thursday' then 0  -- or perhaps 7
                   when 'Friday' then 6
                   when 'Saturday' then 5
                   when 'Sunday' then 4
                end),
               datecol
              ) as the_thursday

If non-English support is needed, you can write locale-independent code using format().

  • Related