SQL Server ASPDiscussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server ASP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
You have to have some FIELD in your TABLE that holds the date entered.
Databases do *NOT* do that automatically.
*ASSUMING* you have such a field:
Code:
SELECT * FROM mytable
WHERE theDateRecordWasAdded >= DATEADD(d,-7,getDate())
Now, the problem with that is that getDate() returns a date AND time, equivalent to NOW() in VB/VBScript. So that DATEADD will give you a date and time exactly 7 days ago as of this moment. If you want it to be 7 days ago, starting at midnight on that day, we have to get fancier.
But I won't go further until you tell me (a) you do have a field in your table that holds when the record was added, (b) whether that field holds a date-only or a date+time value, and (c) how you want to "cut off" the records [to the second or to the day].
I just realized that you posted in the "SQL Server ASP" forum, so I assume you are using ASP to drive all this, right?
So we can do it easier by letting ASP code do some of the work:
Code:
<%
...
weekAgo = DATE() - 7 ' really! it's this simple in ASP code!
SQL = "SELECT * FROM mytable WHERE dateAdded >= '" & weekAgo & "'"
Set RS = yourAlreadyOpenConnection.Execute( SQL )
...
If you do it all in the SQL query it's much messier:
Code:
SELECT * FROM mytable
WHERE dateAdded >= DATEADD( d, -7, CONVERT(DATETIME,CONVERT(VARCHAR(20),getDate(),112),112) )
That's because SQL Server has no simply way to get the current date (getDate() is the equivalent of NOW() in VBS) so you have to do that messy double CONVERT.
The Following User Says Thank You to Old Pedant For This Useful Post: