DateDiff help to verify my code is correct or not?

  • 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

  • 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.

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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 (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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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

  • 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

  • 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.

  • 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. :w00t:

    _______________________________________________________________

    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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply