Urgent help need to verify DateDiff is correct?

  • Hi Friends,

    my requirement is-

    "SmartLaborII Work Order End Date" is NOT greater than "SmartLaborII Previous Work Order End Date"]-

    and the code I'm using is-

    //

    datediff(day,[WorkForce_WorkOrder].[Work Order End Date_WO]

    ,[WorkForce_WorkOrder].[Previous Work Order End Date_WO] )

    >= '0'

    //

    Since I'm new to TSQL world, wanted to know if my above code for my requirement is correct ? if not what should be the right code and why please?

    Kind Regards

    Dhananjay

  • If they are DATE or DATETIME data types why not just do this?

    [WorkForce_WorkOrder].[Work Order End Date_WO] <= [WorkForce_WorkOrder].[Previous Work Order End Date_WO]

  • I would prefer datediff since that way not dependent of type of date format in DB hence need help please:-)

  • dhananjay.nagarkar (3/7/2013)


    I would prefer datediff since that way not dependent of type of date format in DB hence need help please:-)

    If the columns are defined as one of the date data types, you don't need to use DATEDIFF. You can compare dates just like you can compare integers values.

  • Hi Lynn,

    that will not help since in my case one of the dates can remian NULL hence if that's the case then it will not work using > , = etc; hence datediff

    thanks

    Dhananjay

  • dhananjay.nagarkar (3/7/2013)


    Hi Lynn,

    that will not help since in my case one of the dates can remian NULL hence if that's the case then it will not work using > , = etc; hence datediff

    thanks

    Dhananjay

    If either if the date values is NULL, then it will fail the test either way, with or without DATEDIFF.

    You need to explain what you want to happen if one or both of the values is NULL.

  • observe:

    declare @TestDate datetime;

    select datediff(dd,getdate(),@TestDate)

    select datediff(dd,@TestDate,getdate())

    set @TestDate = '20130318';

    select datediff(dd,getdate(),@TestDate)

    select datediff(dd,@TestDate,getdate())

    -----------

    NULL

    (1 row(s) affected)

    -----------

    NULL

    (1 row(s) affected)

    -----------

    11

    (1 row(s) affected)

    -----------

    -11

    (1 row(s) affected)

  • Thanks Lynn you are right

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

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