Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Get Data By Month

Hello,

I know how to get data from the last 30 days using something like the code below...

SELECT * FROM userdata
WHERE DATEDIFF(day, POST_DATE, GetDate()) <= 30

but how can I get the data for just the current month thus far.  For example, if it's May 9, select all posts from May 1-9 and do the same no matter what day it is (May 1-15, or May 1-30).

Thanks,
MT
0
movoni
Asked:
movoni
  • 2
1 Solution
 
rafranciscoCommented:
Try this:

SELECT * FROM UserData
WHERE MONTH(POST_DATE) = MONTH(GETDATE()) AND
           YEAR(POST_DATE) = YEAR(GETDATE())
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
In Access VBA it would be WHERE DatePart("m", DATE_DIFF) = 5
0
 
rafranciscoCommented:
Or this:

SELECT * FROM UserData
WHERE POST_DATE >= DATEADD(D, -(DAY(GETDATE()) + 1), GETDATE())
0
 
movoniAuthor Commented:
Cool... that did it.  Thanks!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now