Return records of last 6 months

Below is my SQL statement

DECLARE @dStart datetime ,
    @dEnd  datetime

SET @dEnd = GETDATE()
SET @dStart = DATEADD(mm, -6, @dEnd)

Select * from MyTable
Where TheDate Between @dStart AND @dEnd

This will return all the records from today minus 6 months data.

But I want this months data plus only the previous 5 months data.

Currently it will return records from March as well.

Answers


Instead of

DATEADD(mm, -6, @dEnd)

You might use

dateadd(month, datediff(month, 0, @dEnd) - 5, 0)

This will truncate date to first of current month and substract five months from it.


Need Your Help

How to resolve a dependency issue

ruby-on-rails rubygems dependencies gem

I am not entirely sure if this can even be done , but this is my error :

Session not saving when moving from ssl to non-ssl

php cakephp session

I have a login screen that I force to be ssl, so like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.