Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

datediff include 1 hour before midnight Expand / Collapse
Author
Message
Posted Tuesday, April 8, 2014 9:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 3:53 PM
Points: 12, Visits: 67
Hi,

I need to find all records for all 3 shifts in 1 day and the datediff below does not include the 1 hour before midnight (missing 1 hour for graveyard shift).

where DateDiff(day, Start_DateTime, '2014-04-07')=0 AND DateDiff(day, End_DateTime, '2014-04-07')=0

Thank You for you time.
Post #1559553
Posted Tuesday, April 8, 2014 11:06 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:34 PM
Points: 20,676, Visits: 32,269
awcrestview (4/8/2014)
Hi,

I need to find all records for all 3 shifts in 1 day and the datediff below does not include the 1 hour before midnight (missing 1 hour for graveyard shift).

where DateDiff(day, Start_DateTime, '2014-04-07')=0 AND DateDiff(day, End_DateTime, '2014-04-07')=0

Thank You for you time.


Based on the date 2014-04-07 what are the dates and times of the shifts you are trying to return.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1559591
Posted Tuesday, April 8, 2014 11:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 3:53 PM
Points: 12, Visits: 67
Thank You for your reply.

I need records for user selected date. In this example I'm using 4/7/14.

The shifts are;
grave = 11pm to 7am
day = 7am to 3pm
swing = 3pm to 11pm

thanks
Post #1559607
Posted Tuesday, April 8, 2014 11:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 3:53 PM
Points: 12, Visits: 67
The start_datetime for this example would be graveyard shift which starts at 11pm on 4/6/14.

Thank You
Post #1559613
Posted Tuesday, April 8, 2014 12:01 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:34 PM
Points: 20,676, Visits: 32,269
awcrestview (4/8/2014)
The start_datetime for this example would be graveyard shift which starts at 11pm on 4/6/14.

Thank You


Okay, so what you are looking for is the following based on a the date 2014-04-07 is this:

where startdate >= '2014-04-06 23:00' and enddate < '2014-04-07 23:00'

Is this a good paraphrase of what you need? If so, then something like this:

WHERE startdate >= dateadd(hour, -1, dateadd(day, datediff(day, 0, getdate()), 0)) and enddate < dateadd(hour, -1, dateadd(day, datediff(day, 0, getdate()), 1))


This assumes the getdate() returns '2014-04-07 hh:mm:nnn.nn'.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1559630
Posted Tuesday, April 8, 2014 12:47 PM This worked for the OP Answer marked as solution
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 3:53 PM
Points: 12, Visits: 67
Lynn,

Thanks for you help Lynn, you're awesome. Below is from your recommendation with minor change to get what I needed.

WHERE start_datetime >= dateadd(hour, -1, dateadd(day, datediff(day, 0, (getdate)), 0)) and DateDiff(day, End_DateTime, 'getdate')=0

Post #1559659
Posted Tuesday, April 8, 2014 1:09 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:34 PM
Points: 20,676, Visits: 32,269
awcrestview (4/8/2014)
Lynn,

Thanks for you help Lynn, you're awesome. Below is from your recommendation with minor change to get what I needed.

WHERE start_datetime >= dateadd(hour, -1, dateadd(day, datediff(day, 0, (getdate)), 0)) and DateDiff(day, End_DateTime, 'getdate')=0



Just realize that this part of the where clause, DateDiff(day, End_DateTime, 'getdate') = 0, is not SARGable meaning you will not make use of any indexes. This part requires the every row needs to be tested to determine if the difference in days between End_DateTime and GETDATE() = 0.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1559671
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse