Solved

Getting everything that is = 24 hours from getdate() using a stored procedure.

Posted on 2008-10-04
7
318 Views
Last Modified: 2012-05-05
I have a scheduled task that runs hourly. Currently it selects items where A.PickupDateTimeScheduled  is within the hour. What I need is to have the stored procedure attached select items that are += to 24 hours, but not 25 hours because the process should pick those items up the following hour. The minutes I am not too concerned about. I hope I am clear in what I am looking for. Thanks in advance for any assistance.
CREATE procedure [dbo].[usr_sel_reservations_12hours]  

as

Set NoCount On

Select Distinct A.ResNO, A.Pickup_City, A.VehicleNoActual As 'VehicleNumber', A.PreferredVehicleType As 'VehicleType', A.NumOfPassengers, A.PassengerFirstName, A.PassengerLastName, A.PickupDateTimeScheduled, A.Pickup_AddressOnLocation, A.Pickup_City,A.Pickup_State, A.Pickup_StreetName, A.Pickup_StreetNo, A.Pickup_ZipCode, A.Pickup_DispatchZoneState, A.Pickup_FlightAirlinePK, A.Pickup_FlightNo, A.Pickup_FlightType, A.Pickup_PlaceDataSource,

				A.Pickup_LandmarkId, A.Pickup_LandmarkName,A.Dropoff_FlightNo, A.DropOff_AddressOnLocation, A.DropOff_AddressLine2, A.DropOff_PlaceDataSource, A.DropOff_LandmarkName, B.EmailAddress, B.Dropoff_FlightDestinationCity, B.Dropoff_FlightDestinationAirport As 'DestinationAirport', B.Dropoff_FlightDestinationCity As 'FlightDestinationCity', B.Dropoff_FlightFAADepartureTime As 'FlightDepartureTime', A.DropOff_StreetNo, A.DropOff_StreetName, A.DropOff_City, A.Dropoff_State, A.Dropoff_Zipcode,A.DropOff_FlightNo,A.DropOff_FlightAirlinePK

From tbRideResCurMaster A

Join tbRideResCurMasterExt B

On A.ResNo = B.ResNo

Where A.PickupDateTimeScheduled BETWEEN dateadd(hour, -1, getdate()) AND getdate() And B.EmailAddress <> '' And B.EmailAddress Is Not NULL And B.Dropoff_FlightDestinationCity Is Not NULL And B.Dropoff_FlightDestinationCity <> ''

GO

Open in new window

0
Comment
Question by:-cr-
7 Comments
 
LVL 23

Expert Comment

by:adilkhan
ID: 22641601
Where A.PickupDateTimeScheduled BETWEEN DateAdd("hh", -24, A.PickupDateTimeScheduled) AND getDate()
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 22641629
where A.PickupDateTimeScheduled BETWEEN DateAdd("hh", -24, A.PickupDateTimeScheduled) AND getdate()
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 22641632
whoops sorry adilkhan yours not there when i posted
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 51

Expert Comment

by:Mark Wills
ID: 22645040
Would be inclined to simply do :

Where A.PickupDateTimeScheduled >= DateAdd("hh", -24, getDate())

0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22645047
Hang on, might have misread the requirement - could you please describe :  that are += to 24 hours ?  Seems that it is going to do a lot of repeat work the next hour it runs, are you saying that every hour it should run for a period which is "yesterday" ? Or am I thinking too much again...
0
 

Author Comment

by:-cr-
ID: 22670084
Yes the samples do not do what I need. I think I am close to getting this to work the way I want.
I have attached a snippet I have been working on, but I need it to do a round to the nearest hour which I believe it is doing. I also need it to then add 24 hours so I am only grabbing reservations exactly 24 hours in advance.
DateAdd(mi, Round(DateDiff(mi,0, A.PickupDateTimeScheduled ) / 30,0) * 30,0) = DateAdd(mi, Round(DateDiff(mi,0, GetDate()) / 30,0) * 30,0)

Open in new window

0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 22671345
OK, so it should be 24 hours from "now", so, if today (gmt +10:00) is 9th October at 5:00Am, then we want pickupdatetimescheduled that is 10th October 5:00am - correct ?

first try :   select convert(varchar(13),getdate() + 1,120)    -- gives you "yyyy-mm-dd hh"

then try:  Where convert(varchar(13),A.PickupDateTimeScheduled,120) = convert(varchar(13),getdate() + 1,120)


0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now