Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 29th, 2010, 05:33 PM
Authorized User
Points: 405, Level: 7
Points: 405, Level: 7 Points: 405, Level: 7 Points: 405, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old November 29th, 2010, 07:33 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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].
Reply With Quote
  #3 (permalink)  
Old November 30th, 2010, 03:47 AM
Authorized User
Points: 405, Level: 7
Points: 405, Level: 7 Points: 405, Level: 7 Points: 405, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #4 (permalink)  
Old November 30th, 2010, 03:00 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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.
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
adamhw (December 1st, 2010)
  #5 (permalink)  
Old January 15th, 2011, 02:44 PM
Authorized User
Points: 405, Level: 7
Points: 405, Level: 7 Points: 405, Level: 7 Points: 405, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #6 (permalink)  
Old January 15th, 2011, 03:20 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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".
Reply With Quote
  #7 (permalink)  
Old January 16th, 2011, 12:22 PM
Authorized User
Points: 405, Level: 7
Points: 405, Level: 7 Points: 405, Level: 7 Points: 405, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
Reply


Thread Tools
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 Off
Pingbacks are On
Refbacks are Off

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 01:34 AM
Using DateDiff iancrabtree Access 2 November 27th, 2005 06:33 PM



All times are GMT -4. The time now is 11:26 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.