EDIT: Nevermind, figured it out...Help with this basic query in SQL Server?

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
I have a report that runs monthly and posts prior month statistics. As of this month, it is not populating my tables. Here is my query.

Code:
[FONT=Courier New]
INSERT DVMS_FUEL_DRIVERS_FINAL (MonthYear,Weight,DriversFueling)[/FONT]
[FONT=Courier New]SELECT MonthYear,Weight,DriversFueling
FROM viewFuelDriversFinal
[B]WHERE month(MonthYear) = month(getdate())-1[/B][/FONT]
I will step through how I think the bold line is processing.

month(12/1/2010) = month(1/11/2011) - 1
12 = 1 - 1
12 = 0

Is this a correct assumption? How can I get it to pull '12' instead of '0'?

Edit:
I decided just to hard code in the 12 for now. This is a once a year occurrence so I'll just suck it up :p
 
Last edited:

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
look into the SQL DATEADD function

DATEADD(mm,-1, getdate()) is what you are looking for
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
It looks like you worked around the problem, rather than figuring it out as the edited title implies, so I will leave the thread open unless you want it closed.
 

ForumMaster

Diamond Member
Feb 24, 2005
7,797
1
0
I encountered a similar problem. What you need to use, is the Datediff function.

Its parameters are as follows: datediff(datepart,startdate,enddate)
Example would be: datediff(month,getdate(),MonthYear)

:)
 
Last edited: