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
?
Use the DATEADD/DATEDIFF trick:
SELECT
DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0),
DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With