# DATEDIFF Calculation Where One Argument is NULL

Hello EE,

I am trying to write a query where I can calculate the date difference between the date a widget arrived in my facility and the day it shipped.  The problem is some widgets have to be repaired before they are shipped.  Therefore, some of the widgets have a NULL ship date.

The query so far...

SELECT WidgetModel, datediff(d, max(DockDate), max(ShipDte)) + 4 as SLAInDays
FROM dbo.Widgets
WHERE (DockDate BETWEEN '3/15/2006' AND '3/20/2006') and (shipdte between '3/15/2006' and '3/20/2006')

I need to be able to add an argument in the query for the NULL shipdates, like:

case when isNULL(shipdte, 'GetDate()') or something to that affect.

Any help is greatly appreciated.

Thanks,

Craig
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Try:

SELECT WidgetModel, datediff(d, max(ShipDte), max(isnull(DockDate, getdate()))) + 4 as SLAInDays
FROM dbo.Widgets
WHERE (ShipDte BETWEEN '3/15/2006' AND '3/20/2006') and (shipdte between '3/15/2006' and '3/20/2006')
0
Commented:
Sorry, I made a paste error and also overlooked something in your requirements.  I will post back shortly
0
Author Commented:
Actually, your query was absolutely correct, except for the order of the data fields.

I did this and it worked perfectly.

SELECT WidgetModel, datediff(d, max(DockDateDte), max(isnull(ShipDte, getdate()))) + 4 as SLAInDays
FROM dbo.Widgets
WHERE (ShipDte BETWEEN '3/15/2006' AND '3/20/2006') and (shipdte between '3/15/2006' and '3/20/2006')
Group By WidgetModel

I think that's right.  I'll wait for your re-post before awarding the points.

Craig

0
Commented:
Your aggregate calculation will give you the difference between the latest DockDate and the latest ShipDate plus 4

Is that what you wanted?  Or were you trying to get the maximum difference between those dates for any single transaction?
0
Author Commented:
I am looking to get the average SLA in days grouped by the Model for everything sent to the facility during a date range.
0
Commented:
In that case I think it should be more like this:

SELECT WidgetModel, AVG(datediff(d, (Crtd_DateTime),(isnull(User8, getdate())))) + 4 as SLAInDays
FROM dbo.Widgets
WHERE (DockDate BETWEEN '3/15/2006' AND '3/20/2006') and (shipdte between '3/15/2006' and '3/20/2006')
Group By WidgetModel

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
That works great too.  I believe the average is a much better representation of the actual number of days between the day it arrived in our facility and the day it shipped back out.

What was happening in the aggregate function without the AVG?  It wasn't averaging?

I will await your reply and then award you the points.  Thanks so much for your help with this.

It is very appreciated.

Craig
0
Commented:
Before you were taking the diffence between the most recent DockDate and the most recent ShipDate and then adding 4.  In case of a null shipdate, today's date is substituted.

Since they were independantly calculated aggregates (MAX), the calculation is not being made on the same row in your table.

The second method will calculate the difference for each row, again substituing nulls with today's date.  Then the average is calculated on the sum of those differences.  We are also adding 4 to that number.  Why we are adding 4?  I have no idea, but I'm sure you have a reason.
0
Author Commented:
Thank you for the explanation.  That makes a lot of sense.  The reason I am adding 4 is to buffer in the shipment transit time.  Thanks again for all of your help.

Regards,

Craig
0
Commented:

I see you accepted my answer and even awarded me a grade of "A" (THANK YOU!!) when I once again committed a paste error, leaving my test fields in the answer (Crtd_DateTime, User8).  Thankfully you were able to work with it without missing a beat.
0
Author Commented:
You are very welcome.  I noticed your test fields in the query, so I replaced them with my datafields.  I knew what you meant.  You deserved an A for your immediate response and quick resolution.

This is what makes the EE community great.

Craig
0
Commented:
Well you have an understanding for the predicament of experts.  We try to work quickly because as you know, "the early bird gets the worm".  We don't want you to get any worms in your Apps though!

lol

Peter
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.