Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 12th, 2005, 06:37 PM
Pluto Pluto is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 Pluto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 8 h 25 m 59 sec
Reputation Power: 5
Smile Working with dates in SQL strings

Hi there,

I wonder if you could help, I am trying to extract records from my database which are BETWEEN two dates. The date field in my databse contains the dates in Short date format, like dd/mm/yyyy.
The SQL string I am using is as follows, but it just does not seem to work! It doesnt give any errors, but just doesnt extract any records (which definitely exist).

...FROM Advertisers WHERE Paid = 'yes' AND (dateAdded BETWEEN " & formatDateTime("01/01/05",2) & " AND " & formatDateTime("01/01/06",2) & ")"

I am new to working with dates so there is obviously something I am doing wrong (
Your help would be so much appreciated, manyyyy thanksss

Reply With Quote
  #2  
Old March 12th, 2005, 07:24 PM
selwonk's Avatar
selwonk selwonk is offline
Contributing User
ASP Free Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2004
Posts: 2,944 selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 10 h 43 m 32 sec
Reputation Power: 98
Try doing a Response.Write(...) of your SQL statement and check that the FormatDateTime(...) has given the expected date format
__________________
selwonk

If I've posted some code above, you might think it looks a bit simplistic. It might be. I'd rather people tried the next step themselves rather than getting a full solution on a plate. That way they learn more!

Reply With Quote
  #3  
Old March 12th, 2005, 07:40 PM
Pluto Pluto is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 Pluto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 8 h 25 m 59 sec
Reputation Power: 5
Tried response.write on SQL String

Hi there, thank you so much for replying.

Yeah I did try that, and that seems fine to me... It writes the following :

SELECT SUM(Fee) FROM Advertisers WHERE Paid = 'yes' AND (dateAdded BETWEEN 01/01/2005 AND 01/01/2006)

Is there something I am maybe missing?

Thank you again

Reply With Quote
  #4  
Old March 12th, 2005, 07:48 PM
Pluto Pluto is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 Pluto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 8 h 25 m 59 sec
Reputation Power: 5
Problem applying SUM function maybe...

I think I have seen something.... I am basing it on the field dateAdded but on a SUM function... could this be the problem? If it is, how would I get around this? Would I have to manually code the summing up as I loop through the records, or can I still apply the SUM function somehow?

Thank you again

Reply With Quote
  #5  
Old March 13th, 2005, 07:19 AM
selwonk's Avatar
selwonk selwonk is offline
Contributing User
ASP Free Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2004
Posts: 2,944 selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 10 h 43 m 32 sec
Reputation Power: 98
The portion of the SQL before the WHERE is what you are asking it to return. The portion after the WHERE is the selection criteria. The two can be completely unique. It should be possible to do what you are doing

You could try changing your code to produce the SQL statement like this:

SELECT SUM(Fee) FROM Advertisers WHERE Paid = 'yes' AND (dateAdded BETWEEN '01/01/2005' AND '01/01/2006')

You just need to add the ' characters

Reply With Quote
  #6  
Old March 13th, 2005, 01:31 PM
Pluto Pluto is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 Pluto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 8 h 25 m 59 sec
Reputation Power: 5
Cannot get date formatting right...

Thanks again I have tried what you suggested but it still does not seem to return ANY records at all!! I have tried it with so many different combinations...

...WHERE Paid = 'yes' AND (dateAdded BETWEEN " & formatDateTime("01/01/2004",2) & " AND " & formatDateTime("01/01/2005",2) & ")"
...WHERE Paid = 'yes' AND (dateAdded BETWEEN '01/01/2004' AND '01/01/2005')"
...WHERE Paid = 'yes' AND (dateAdded BETWEEN 01/01/2004 AND 01/01/2005)"
...WHERE Paid = 'yes' AND (dateAdded BETWEEN '01-01-2004' AND '01-01-2005')"

And it just will not seem to work... like I say the dateAdded field is in an MS Access database and is formatted as Short date

ANy suggestions would be greatly appreciated. And thanks for the help with the SUM bit, as you can see I am quite new to this
Thank you again.

Reply With Quote
  #7  
Old March 13th, 2005, 01:51 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
dates passed to Access need to be surrounded by # symbols.

Reply With Quote
  #8  
Old March 13th, 2005, 02:20 PM
selwonk's Avatar
selwonk selwonk is offline
Contributing User
ASP Free Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2004
Posts: 2,944 selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 10 h 43 m 32 sec
Reputation Power: 98
Sorry - I presumed this was SQL Server due to the forum it was posted in...

Reply With Quote
  #9  
Old March 13th, 2005, 02:28 PM
Pluto Pluto is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 Pluto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 8 h 25 m 59 sec
Reputation Power: 5
Thank you so much!!

Thank you SO MUCH!!! As you can imagine it now works fine!! I am ever so grateful, I would have been there for many more hours wondering why it did not work!! So for MS Sql databases I containt the date in ' and for Ms Access # ?

Thank you again!!

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Working with dates in SQL strings


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT