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 9th, 2005, 11:14 AM
sayzey sayzey is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: England
Posts: 17 sayzey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 42 m 29 sec
Reputation Power: 0
Question Help with date functions - dateadd

Hey. Thought this would be an easy dateadd function but I cant get it to work!

Background:

Using ASP.Net, VB.NET with SQl server 2000 DB.

I have a table i need to query where I want to display all records between a user defined time and a pre defined time i.e

Display all records between Now and 30 mins time

OR

Display all records between [user entered parameter i.e. 15:00] and 1 hour after user defined parameter.

I want to have the user select in a drop down the time to query from and the amount of time after that. I hope I have explained that well enough! ALL records in the table are in the format 01/01/2005 12:00:00
i.e. there are different times throughout the day, and all of them are on the same day.

On another point is there any way to change this is stored, I only want to bother with time, not dates, at the moment i convert before i display these values.

anyway back to the problem, I have tried the following which I thoguht would work:

Code:

SELECT * FROM tblTimetable
where sourcetime between getdate() and dateadd(hh,01,getdate())



Where sourcetime is a datetimefield with a typical value: 01/01/2005 12:00:00

obviously the getdate() function will show the date and time now today, so how can I get this to work?

Thanks in advance!

Reply With Quote
  #2  
Old March 9th, 2005, 11:57 AM
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
To get all records where SourceTime is between Now and 30 minutes from Now just do this.
Code:
SELECT *
FROM TblTimeTable
WHERE SourceTime BETWEEN GetDate() AND DateAdd(n, 30, GetDate())
Comments on this post
sayzey disagrees: Cheers for the help

Reply With Quote
  #3  
Old March 9th, 2005, 01:02 PM
sayzey sayzey is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: England
Posts: 17 sayzey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 42 m 29 sec
Reputation Power: 0
Hey,

Thanks for that, it works if I change all the dates stored in the database to todays date, however all my dates are in the past, so any ideas how I would be able to use the current time, and a date in the past say: 01/01/2005??

Ive tried a lot but cant seem to get anything that doesnt produce an error.

Reply With Quote
  #4  
Old March 9th, 2005, 02:02 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
To get all records upto Now, just do this
Code:
SELECT *
FROM TblTimeTable
WHERE SourceTime <= GetDate() 

Reply With Quote
  #5  
Old March 9th, 2005, 02:27 PM
sayzey sayzey is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: England
Posts: 17 sayzey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 42 m 29 sec
Reputation Power: 0
hmm let me try and explain again what the problem is:

I have a table named timetable for storing the times when a bus shows up at the stop. The busses come at the same time each day so for each bus I am storing a bunch of times. I dont care about date its irrelevant to the table, however as sql is awkward I HAVE to specify a date for each time, so I chose 2005-01-01, so my table looks like this:

BusTime BusNumber
2005-01-01 09:30 34
2005-01-01 10:30 54
2005-01-01 11:28 89
etc

So I would like to be able to query the table to say display all the buses that run within the next hour, so obviously I want to use the getdate() function + 1hr function somewhere, obviously I arent interested in the date part of this function but only the time. I have tried along the lines of:

Code:
 SELECT sourcetime, busnumber
 FROM tblTimetable
 WHERE sourcetime between '2005-01-01'&(CONVERT(varchar,(GETDATE()),108) AND '2005-01-01'&DATEADD(h,01,GETDATE())
 

Any ideas?

Reply With Quote
  #6  
Old March 9th, 2005, 04:37 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
Try something like this
Code:
SELECT *
FROM tblTimeTable
WHERE BusTime BETWEEN Cast('2005-01-01' + Cast(SUBSTRING(Cast(GetDate() As varchar), 14, 15) As DateTime) As DateTime) AND DateAdd(hh, 01, Cast('2005-01-01' + Cast(SUBSTRING(Cast(GetDate() As varchar), 14, 15) As DateTime) As DateTime))
Comments on this post
gregory.owen@hp agrees: tripple nested casts! Sweet!

Reply With Quote
  #7  
Old March 9th, 2005, 04:46 PM
sayzey sayzey is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: England
Posts: 17 sayzey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 42 m 29 sec
Reputation Power: 0
Thats Spot on Memnoch!! You come across all EVIL!! But your good really!! Very good! that code is a bit of a mouthfull but it works!!!

Thank you!!

Reply With Quote
  #8  
Old March 15th, 2005, 11:23 PM
gregory.owen@hp's Avatar
gregory.owen@hp gregory.owen@hp is offline
Maniac
ASP Free Novice (500 - 999 posts)
 
Join Date: Sep 2003
Location: Sweet Home, Oregon
Posts: 548 gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level)gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level)gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level)gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level)gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 4 h 37 m 8 sec
Reputation Power: 14
Not to try and outdo the great and glorious Memnoch, but if you have the access to create and execute stored procedures on your database, you can make your job far easier by creating a sp like this:

Code:
CREATE PROCEDURE dbo.getBusSchedule (@hours as int)
AS
declare @now as DateTime
declare @referenceTime as DateTime

set @now = getDate()
set @referenceTime = dateadd(dd,datediff(dd,@now, cast('1/1/2005 12:00:00 am' as DateTime)),@now)

select *from tblTimeTable where BusTime  between @referenceTime and dateadd(hh,@hours,@referenceTime)
GO


Then in your .net code, you can simply call the sp: "getBusSchedule 1" for one hour from now, or pass any number of hours you want: "getBusSchedule -3" would give you the buses for the previous 3 hours and so forth.

Notice that I also give you one additional option for creating a specific time on the given date based on the current time. This may be easier to grasp than all of the fancy casting in Memnoch's post. (some copying and pasting will easily translate this into in-line sql if you can't use stored procedures -- but PLEASE look into using stored procedures instead.)
Comments on this post
Memnoch agrees: Using stored procedures is the recommended way. Nice code too!

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Help with date functions - dateadd


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 6 hosted by Hostway
Stay green...Green IT