Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Show Records Added Last Month

Posted on 2009-05-04
12
Medium Priority
?
394 Views
Last Modified: 2012-05-06
Hi guys,

I am working on my website and on the first page I would like to show CDs that were added within last month. I have DateAdded field in my CDs table. Data type is DATE. I am using SQL Server 2008.

Thank you.
0
Comment
Question by:Yury Merezhkov
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 24299552
this should do:
WHERE DateAdded < CONVERT(datetime, CONVERT(varchar(8), getdate(), 120) + '01', 120)
  AND DateAdded >= DATEADD(month, -1,  CONVERT(datetime, CONVERT(varchar(8), getdate(), 120) + '01', 120))

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24299578
Or like this:
WHERE DateAdded >= DATEADD(month, -1, DATEDIFF(day, 0, GETDATE()-DAY(GETDATE())+1))
AND DateAdded < DATEADD(month, 0, DATEDIFF(day, 0, GETDATE()-DAY(GETDATE())+1))

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24301496
Or:
WHERE DateAdded BETWEEN DATEADD(month, -1, GETDATE() - 1) AND GETDATE()
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24301696
to clarify the difference:

 acperkins's code returns the date between now - 1 month and now, which includes the time:
  2009-04-05 07:10 to   2009-05-05 07:10 (bith inclusive)

 mvisa's and mine return the data of last full month.
  2009-04-01 00:00 (inclusive) to 2009-05-01 00:00  (exclusive)

0
 
LVL 7

Author Comment

by:Yury Merezhkov
ID: 24301706
Ok thank you guys. I will try these tomorrow.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24301737
>>acperkins's code returns the date between now - 1 month and now, which includes the time:<<
Close, but no cigar (you missed the -1).
If anything it should be:
2009-04-04 07:10 to   2009-05-05 07:10 (bith inclusive)

Since they are DATE and not datetime the time is not relevant.  What is important is the definition of "added within last month".  If they mean In the month of April then you are of course right.  I interpreted it as within the last month, in other words it would pick up any days (using your time zone) between 2009-04-05 and 2009-05-05.

So it is all a question of semantics...
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 400 total points
ID: 24301766
On second thoughts perhaps yor misterpretation of the results was closer to what I intended then my code was :)  In other words it should have been:
WHERE DateAdded BETWEEN DATEADD(month, -1, GETDATE()) AND GETDATE()

Which would give the results you advertised:
2009-04-05 07:10 to  2009-05-05 07:10
And would cover any date between 2009-04-06 and 2009-05-05

And no doubt the purist will point out that there could be a problem at midnight. Then of course I would have to ask them to focus on "I am working on my website and on the first page I would like to show CDs", so we are not talking about eBay here.
0
 
LVL 7

Author Comment

by:Yury Merezhkov
ID: 24305940
Guys, I am so sorry but since I had a lot of trouble with SQL Server I decided to use Access 2007 instead. I tried changing GETDATE() to NOW() but I get an error "No value given for one or more required parameters" on all statements that you have provided.
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1200 total points
ID: 24306693
Try this for Access:



WHERE DateAdded >= DATESERIAL(YEAR(NOW()), MONTH(NOW())-1, 1)
AND DateAdded < DATESERIAL(YEAR(NOW()), MONTH(NOW()), 1)

Open in new window

0
 
LVL 7

Author Closing Comment

by:Yury Merezhkov
ID: 31577777
Thanks a lot!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24307032
RealSnaD,
Don't forget that in MS Access DateAdded will be defined as datetime instead of date.  So you should take care to always set it to midnight when saving or you will have to modify your query.  For example, if you are using mwvisa1 solution you could modify it as follows:
WHERE DateAdded >= DATESERIAL(YEAR(Date()), MONTH(Date())-1, 1)
AND DateAdded < DATESERIAL(YEAR(Date()), MONTH(Date()), 1)
 
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24307058
Please ignore my last comment, I have no idea what I was thinking. :)
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question