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 help to verify my code is correct or not? Expand / Collapse
Author
Message
Posted Thursday, March 14, 2013 2:44 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, October 27, 2014 6:52 AM
Points: 90, Visits: 182
Hi Friends,

I'm using DateDiff function -

my requirement is -

1) "SmartLaborII Job Posting End Date"" is NOT < ""SmartLaborII Work Order End Date"

and
my code is -
--------
not(datediff(day,[WorkForce_JobPosting].[Job Posting End Date_JP]
,[WorkForce_WorkOrder].[Work Order End Date_WO])
> 0)
--------

2) "SmartLaborII Work Order End Date"" is NOT greater than ""SmartLaborII Previous Work Order End Date"

my code is -

///

datediff(day,[WorkForce_WorkOrder].[Work Order End Date_WO]
,[WorkForce_WorkOrder].[Previous Work Order End Date_WO] )
>= 0
//

Need help from experts to verify if my code for the 2 requirements is correct ?

Kind Regards
Dhananjay
Post #1431270
Posted Thursday, March 14, 2013 2:54 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 3:12 AM
Points: 20,803, Visits: 32,732
This is the fourth or fifth thread you have started for the same question. Creating new threads every day or so isn't going to get you more help. Keep the same question in one place. it keeps all the responses you do get together instead of fragmenting them across multiple threads.

We have tried to answer this for you before but I don't recall you ever answering the question about how you want to handle null values in either or both of the columns you are comparing. Without that informationi e really can't help you.



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 #1431272
Posted Thursday, March 14, 2013 2:58 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:54 PM
Points: 13,228, Visits: 12,705
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable.

I think that something like this should meet your requirements. Of course as Lynn stated this doesn't accomodate NULL values in any of those columns.

[WorkForce_JobPosting].[Job Posting End Date_JP] < [WorkForce_WorkOrder].[Work Order End Date_WO]
AND
[WorkForce_WorkOrder].[Previous Work Order End Date_WO] <= [WorkForce_WorkOrder].[Work Order End Date_WO]



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1431273
Posted Thursday, March 14, 2013 3:01 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 3:12 AM
Points: 20,803, Visits: 32,732
Sean Lange (3/14/2013)
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable.

I think that something like this should meet your requirements. Of course as Lynn stated this doesn't accomodate NULL values in any of those columns.

[WorkForce_JobPosting].[Job Posting End Date_JP] < [WorkForce_WorkOrder].[Work Order End Date_WO]
AND
[WorkForce_WorkOrder].[Previous Work Order End Date_WO] <= [WorkForce_WorkOrder].[Work Order End Date_WO]



I gave him basically the same answer in another thread. He didn't like the answer because one or the other of the columns can be null. With that said, DATEDIFF suffers the same problem if a column is null.



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 #1431274
Posted Thursday, March 14, 2013 3:11 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:54 PM
Points: 13,228, Visits: 12,705
Lynn Pettis (3/14/2013)
Sean Lange (3/14/2013)
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable.

I think that something like this should meet your requirements. Of course as Lynn stated this doesn't accomodate NULL values in any of those columns.

[WorkForce_JobPosting].[Job Posting End Date_JP] < [WorkForce_WorkOrder].[Work Order End Date_WO]
AND
[WorkForce_WorkOrder].[Previous Work Order End Date_WO] <= [WorkForce_WorkOrder].[Work Order End Date_WO]



I gave him basically the same answer in another thread. He didn't like the answer because one or the other of the columns can be null. With that said, DATEDIFF suffers the same problem if a column is null.


Having seen your post I went and found the other thread(s). I was laughed that we both posted code that was so nearly identical you can't tell the difference.

Not only does the DATEDIFF version not handle NULLS and render indexes useless, the most important flaw is that it doesn't actually meet the requirements if the dates are on the same day. In other words using datediff we could have [Job Posting End Date_JP] being greater than [Work Order End Date_WO] but the datediff logic would say it is ok.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1431278
Posted Thursday, March 14, 2013 5:10 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, October 27, 2014 6:52 AM
Points: 90, Visits: 182
Hi Sean,

When you say-

"
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable"

Can you please help me understand why it does not meet my requirement?
Also Why and under what scenarios / situtations DateDiff function needs to be used and need not be used?
The solution you and Lynnhave provided that I already had beven before I joined this site, but using Predicates comparison for dates is not allowed in my team I have been asked to use DateDiff hence would be much obliged if you can justiy your reason and help me learn please so that I can convince my manager :)

Kind Regards
Dhananjay
Post #1431300
Posted Thursday, March 14, 2013 7:30 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 3:12 AM
Points: 20,803, Visits: 32,732
dhananjay.nagarkar (3/14/2013)
Hi Sean,

When you say-

"
I haven't seen all the other threads but from what you posted I can't figure out why you are using DATEDIFF at all. It doesn't meet your requirements and it will make your query nonSARGable"

Can you please help me understand why it does not meet my requirement?
Also Why and under what scenarios / situtations DateDiff function needs to be used and need not be used?
The solution you and Lynnhave provided that I already had beven before I joined this site, but using Predicates comparison for dates is not allowed in my team I have been asked to use DateDiff hence would be much obliged if you can justiy your reason and help me learn please so that I can convince my manager :)

Kind Regards
Dhananjay


SARGable, meaning SQL Server will use an index on the table if one exists that benefits the query. Using DATEDIFF the way you are using it will force SQL Server to use a table scan and process every row of the table(s) to determine if the criteria is met for the records to be included in the result set.

As for what DATEDIFF will do, run the following:


declare @Date1 datetime, @Date2 datetime;
set @date1 = '20130314 19:23:30.000';
set @date2 = '20130314 19:23:45.000';
-- By observation @date1 is less than @date2

select
@date1,
@date2,
case when @date1 < @date2 then '@date1 < @date2' else '@date2 <= @date1' end, -- will display @date1 < @date2: true
case when datediff(day,@date1,@date2) > 0 then '@date1 < @date2' else '@date2 <= @date1' end -- will display @date2 <= @date1: not true





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 #1431319
Posted Friday, March 15, 2013 7:42 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 3:12 AM
Points: 20,803, Visits: 32,732
By the way, you still haven't told us how you want to handle situations where one or both of the dates are null. If date1 is null and date2 is not null, date1 is not null and date2 is null, both date1 and date2 are null. We need this information to provide a complete answer.


What would really help, is the DDL (CREATE TABLE statement(s)) for the table(s) involved, sample data for the table(s) as a series of INSERT INTO statements, expected results based on the sample data, and the code you have written so far to solve your problem.

If you need help with all this, take the time to read the first article I reference below in my signature block regarding asking for help. It will walk you through what you need to post and how to do it.



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 #1431535
Posted Friday, March 15, 2013 7:54 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:54 PM
Points: 13,228, Visits: 12,705
Dhananjay - did the example and explanation Lynn posted help you understand? The problem with datediff is calculates differences on a boundary. The results is how many of those boundaries are crossed between the two dates. In the example Lynn posted the datediff(DAY...) will return 0 but clearly @date2 > @date1.


using Predicates comparison for dates is not allowed in my team


This is obviously based on the fact that your manager does not understand how this type of thing works. You are forbidden to use best way for data comparison.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1431543
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse