Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Get the first occurance of the 15th for a date

I need to write a function or SP that will return the first occurance of the 15th. For example, if I pass the date as May 8th, then it should return May 15th. If I pass May 30th, then it should return June 15th.

like image 901
user749448 Avatar asked May 13 '11 14:05

user749448


1 Answers

One way

   DECLARE @d DATETIME
    SELECT @d = '20110508'
    --SELECT @d = '20110530'


    SELECT  CASE WHEN DAY(@d)  > 15 
    THEN  dateadd(mm, datediff(mm, 0, @d)+1, 0) + 14
    ELSE dateadd(mm, datediff(mm, 0, @d)+0, 0)+ 14 end
like image 83
SQLMenace Avatar answered Sep 19 '22 00:09

SQLMenace