0

I have a small idea for that but i don't know how to interpret it in sql server (MSDN).

if (Month < ActualMonth)
{
  (StartDateReq = FirstDayMonth);
  (EndDatereq) = LastDayMonth);
}
else if (Month = currenTimeStamp)
 {
  (EndDateReq = currentTimeStamp);
 }

2 Answers2

0

If you have a date in SQL Server, you can get the first and last day of the month using:

select dateadd(day, 1 - day(datecol), datecol) as firstday,
       dateadd(day, -1, dateadd(month, 1, dateadd(day, 1 - day(datecol), datecol))) as lasstday

In SQL Server 2012+, you can use the function EOMONTH() for the last day of the month.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Drawing on Gordon's answer:

DECLARE @myDate Date = '2015-10-25'

select DATEADD(DAY, 1 - DAY(@myDate), @myDate) as firstday,
       CASE WHEN YEAR(@myDate) = YEAR(GETDATE()) AND 
                 MONTH(@MyDate) = MONTH(GETDATE()) 
       THEN @MyDate
       ELSE DATEADD(DAY, -1, DATEADD(MONTH, 1, DATEADD(DAY, 1 - DAY(@myDate), @myDate))) 
       END as lasstday
Steve Ford
  • 7,433
  • 19
  • 40