If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > How to pull current month data from table updated daily

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Dec 2012
Posts: 11
How to pull current month data from table updated daily

Hey folks, novice programmer, first time forum user:

I have a table thats updated daily with monthly data totals:
Month, Total orders1, Total orders2, etc
12/01/2012, 5, 8, etc
11/01/2012, 6, 5, etc

How do I pull data from this table in SQL Server for ONLY the current month? I was thinking using the getdate() function to get the current month, but it doesn't match exactly so I get no results
Reply With Quote
  #2 (permalink)  
Old
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 5,391
Code:
select *
from table
where [month] = convert(date, dateadd (dd, -1 * datepart(dd, getdate()) + 1, getdate()))
By the way, "Month" is a reserved word in SQL Server.
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Dec 2012
Posts: 11
Thank you, the column name is actually ordermonth, I was just trying to shorten it a bit.
I tried that code but got this error msg:

Server: Msg 243, Level 16, State 1, Line 1
Type date is not a defined system type.
Reply With Quote
  #4 (permalink)  
Old
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 12,524
where OrderMonth >= DATEADD(month, DATEDIFF(month, '1900-01-01', getdate()), '1900-01-01') --Returns current month
and OrderMonth < DATEADD(month, DATEDIFF(month, '1900-01-01', getdate())+1, '1900-01-01') --Returns next month
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
www.LobsterShot.blogspot.com
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Dec 2012
Posts: 11
That worked perfectly! Thank you!!
One question tho..I figured out this little bit on my own and it seems to work:

WHERE datepart(mm,ordermonth) =
(SELECT datepart(mm,getdate()))

Is there any reason why this would be any better or worse than Blindman's suggestion? Again, I'm brand new at this stuff, so I'm just trying to figure out what does what and how..
Thank you again!

Last edited by JayGrrr; 12-11-12 at 17:09.
Reply With Quote
  #6 (permalink)  
Old
Registered User
 
Join Date: Dec 2012
Posts: 11
Oh, mine returns data for every year w current month in date, not just current month.. Crap. Got it.
Reply With Quote
  #7 (permalink)  
Old
Registered User
 
Join Date: May 2008
Posts: 97
Why do people use system reserved words as a column name.. I learned in college to stay away after spending an entire weekend diagnosing why my access database connection wasn't working with java.

I still feel the pain to this day.
Reply With Quote
  #8 (permalink)  
Old
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 12,524
Because Microsoft reserved all the best words.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
www.LobsterShot.blogspot.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On