I’ve used this technique many times to help grab the beginning and end of a month. It’s useful for queries where you need to use a BETWEEN.
--use @monthsAgo to set which previous month's records to include. Use 0 for current month and use negative numbers for future months
declare @monthsAgo int = 3
declare @StartDate datetime = dateadd(dd,-((day(getdate()))-1),(convert(varchar(12),dateadd(mm,-@monthsAgo,getdate()),101)))
WHERE dateOfSale BETWEEN @dateStart AND @dateStop
I hope this is helpful to you in your querying adventures. Got another way to accomplish the task? I’d love to hear from you. Post a comment below.