Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DATEDIFF Calculation Where One Argument is NULL

Posted on 2006-03-20
12
Medium Priority
?
1,295 Views
Last Modified: 2006-11-18
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
0
Comment
Question by:CraigDerington
  • 7
  • 5
12 Comments
 
LVL 3

Expert Comment

by:langerking
ID: 16240208
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
 
LVL 3

Expert Comment

by:langerking
ID: 16240261
Sorry, I made a paste error and also overlooked something in your requirements.  I will post back shortly
0
 
LVL 1

Author Comment

by:CraigDerington
ID: 16240288
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.

Thanks for your help.

Craig

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:langerking
ID: 16240338
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
 
LVL 1

Author Comment

by:CraigDerington
ID: 16240353
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
 
LVL 3

Accepted Solution

by:
langerking earned 2000 total points
ID: 16240530
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
 
LVL 1

Author Comment

by:CraigDerington
ID: 16240598
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
 
LVL 3

Expert Comment

by:langerking
ID: 16240707
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
 
LVL 1

Author Comment

by:CraigDerington
ID: 16240734
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
 
LVL 3

Expert Comment

by:langerking
ID: 16240827
Glad I could help!

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
 
LVL 1

Author Comment

by:CraigDerington
ID: 16240880
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
 
LVL 3

Expert Comment

by:langerking
ID: 16240951
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
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 shrink a transaction log file down to a reasonable size.
Suggested Courses

810 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