|
SQL Server ASP Discussions 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
November 29th, 2010, 06:33 PM
|
Authorized User
|
|
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
|
|
using datediff
Hi there I want to select records from my database that were entered in the last 7 days. I'm thinking it would b something like this:
strDatenow = now()
SELECT * FROM mytable WHERE DATEDIFF(strDatenow) <= 7
Is this anywhere near where it should be?
thanks
Adam
|
November 29th, 2010, 08:33 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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].
|
November 30th, 2010, 04:47 AM
|
Authorized User
|
|
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
|
|
DateDiff
Great thanks Old Pedant for the detailed response.
The db does hold a datetime type field called dateadded and I would like it to be to the day.
So in answer to your questions:
a) you do have a field in your table that holds when the record was added,
Yes - called 'dateadded'
(b) whether that field holds a date-only or a date+time value, and
holds datetime value
(c) how you want to "cut off" the records [to the second or to the day].
to the day would be great.
|
November 30th, 2010, 04:00 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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:
|
|
January 15th, 2011, 03:44 PM
|
Authorized User
|
|
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
|
|
should this return what I think it should?
Hi Old Pedant,
This is an addition to this thread and was hoping you could clarify this for me:
My sql statement reads: select * from mytable where p.dateadded <= DATEADD(d,-90,getDate()) -
I'm hoping this will return anything from my table older than 90 days old?
Am I correct?
thanks
Adam
|
January 15th, 2011, 04:20 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Yes, but with the caveat that it will be 90 days to the second, not to the day, as I discussed back in November.
So, again, you might be better off doing part of the work in ASP code:
Code:
SQL = "SELECT * FROM mytable WHERE dateAdded >= '" & ( DATE() - 90 ) & "'"
Ummm...just noticed that your code had p.dateAdded but no place do you alias any table to "p".
|
January 16th, 2011, 01:22 PM
|
Authorized User
|
|
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
|
|
date diff
thanks Old Pedant, that's great.
Yes, sorry, I do use p. as an alias for a table in the proper sql statement.
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
DateDiff |
zahyea |
.NET Framework 1.x |
0 |
October 16th, 2007 04:38 AM |
DateDiff Question |
ExDb |
BOOK: Beginning VB.NET Databases |
2 |
August 25th, 2007 10:11 PM |
datediff |
ebburks |
Access |
2 |
July 28th, 2006 06:58 PM |
help with DateDiff |
isaac2004 |
Classic ASP Basics |
9 |
January 16th, 2006 02:34 AM |
Using DateDiff |
iancrabtree |
Access |
2 |
November 27th, 2005 07:33 PM |
|
|