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

Install Neo4j REST server plugin

java rest plugins amazon-ec2 neo4j

I'm trying to create sample server plugin for Neo4j REST server based on http://docs.neo4j.org/chunked/snapshot/server-plugins.html

RedirectToAction doesnot change the browser URL

jquery-mobile razor asp.net-mvc-4

I was trying to redirect from the Account controller (Account/Login) to User/Index by doing below syntax.

Uploadify - Random ID returned to form issue

javascript jquery uploadify

I have the following code which is supposed to return a random string back to the form once a file is uploaded. Previously to adding the onUploadSuccess code, it was working OK, but if I tried to u...