Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL return first day of last month and current month @ 00:00:00 [duplicate]

Tags:

sql

I am using this code

declare @mydate datetime select @mydate = getdate()
declare @startdate varchar(20)  SELECT @StartDate = CONVERT(varchar(20),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))-1),DATEADD(mm,-1,@mydate)),120)
declare @enddate varchar(20) SELECT @EndDate = CONVERT(varchar(20),DATEADD(dd,-(DAY(@mydate)-1),@mydate),120)
Select @startdate as 'Start Date', @enddate as 'End Date'

To determine the first day of the previous and current month for a report, but it gives a time based on the current time. How can I get

2011-04-01 00:00:00
&
2011-05-01 00:00:00

?

like image 913
gooddadmike Avatar asked Nov 28 '22 07:11

gooddadmike


1 Answers

Use the DATEADD/DATEDIFF trick:

SELECT
   DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0),
   DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
like image 129
gbn Avatar answered Dec 10 '22 02:12

gbn