Solved

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

Posted on 2008-10-04
7
315 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

706 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