views:

39

answers:

3

Ok I am trying to write a query that says get the current date and make it the start date. Then I want to go a month back from that current date for the EndDate. Is it possible to do this? Like if it was 9-15-2010 as the start date can I go a month back from that to 8-15-2010 or is this no possible....what would you do for like 9-20-2010 as the start date since every month has a different amount of days in it? Otherwise if this is not possible how else could I do this? The report will always be run on the 25th of the month so any ideas? I need to go from the 25th back a month....I can get some duplicate records between months if needed but less is obviously better

Right now I am using this:

DECLARE @StartDate DATETIME,
 @EndDate DATETIME;

SET @StartDate = DATEADD(m,-1,GETDATE());
SET @EndDate = DATEADD(m, 1, @StartDate);

Does this work?

Also, how would I then say my AuditInsertTimestamp is between @Start adn @EndDate?

Currently I have this:

AND cvn.[AuditInsertTimestamp] BETWEEN @StartDate AND @EndDate ;

This is still giving me dates like 7-26-2010 though....

Thanks!

+2  A: 

That should work. Did you try it?

If it doesn't work (and there are only 12 test cases to check if you don't trust the documentation) then you can re-build the date from the date parts.

Here's the problem. It should be like this:

cvn.[Subject] = 'Field Changed (Plate Type)'
    AND (
        cvn.[Note] LIKE 'Old Type: IRP%New Type: BASE PLATE%'
        OR cvn.[Note] LIKE 'Old Type: Base Plate%New Type: IRP%'
    )
    AND cvn.AuditInsertTimestamp BETWEEN GETDATE() AND DATEADD(MONTH, -1, GETDATE()) 

AND takes precidence over OR, so you were picking up anything with Old Type:IRP or in the correct date range (with Old Type: Base Plate)

Kendrick
Yes I did that then I just added to my OP what I used in my where clause and it still gives me dates like 7-26-2010...
As a test case, hard code the two dates to what you think they should be and run the query again. You can also select @StartDate and @EndDate to see what values they're actually set to. The between .. and .. should work, although I think it's inclusive for the first date and exclusive for the second.
Kendrick
The @Start and @End is correct but when I say AuditInsertTimestamp BETWEEN @Start AND @End it doesnt give me the correct data.
Are you selecting AuditInsertTimeStamp and it's not in the range, or are you basing that date on another field in the table? Is AuditInsertTimeStamp a DateTime object as well?
Kendrick
Yes AuditInsertTimestamp is a DateTime. And AuditInsertTimestamp is the datetime field that is supposed to be between @Start and @End
my current query is above in OP
The problem is your OR. You're missing brackets.
Kendrick
Brackets around the beginning and end of it?
Around the two "like"s. I messed up the code in my answer, but I'll fix it.
Kendrick
There it is, good catch!
Abe Miessler
Hmmm its returning nothing now but I think thats my problem with one of my notes ok thank you that is very helpful!!!
Its actually that I jsut had the dates switched around in the where clause...got it now...thank you again.
A: 

Yes it should work as expected.

vulkanino
A: 

Based on your comment:

Well this is being used to select records. So if I run it on the 25th I need 30 days back then my field AuditInsertTimestamp needs to be between these 2 dates.

I think you need to do something like this:

SELECT * FROM Table
WHERE AuditInsertTimestamp BETWEEN GETDATE() AND DATEADD(MONTH, -1, GETDATE())
Abe Miessler
This is still giving me 2010-07-20 as some of the dates...hmmmm
Really? What happens when you just do `SELECT GETDATE()`? What is the datatype of `AuditInsertTimestamp`?
Abe Miessler
AuditInsertTimestamp is a DateTime. and Select getdate() = 2010-09-20 09:46:43.087
my current query is above in OP