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 switch between debug/release in Visual C# 2008 Express

c# visual-studio-2008 configuration

I saw How to switch between debug and release in Visual C# Express 2010?, but this didn't give me a clue of how to do it in Visual C# 2008 Express, since there is no such "Expert Settings".

How to run certain script just once at user end?

javascript html

For example, when the user enter URL for the first time,

Array of pointers which point to themselves

c arrays pointers struct

I found an interesting exercise which says the following:

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.