DateDiff function help

  • Hi Friends,

    I'm new to TSQL and in my code I'm using -

    datediff(day,[WorkForce_JobPosting].[Job Posting End Date_JP]

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

    > 0

    the above code if I use with single quotes round 0 still gives me the same result-

    ie,-

    datediff(day,[WorkForce_JobPosting].[Job Posting End Date_JP]

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

    > '0'

    Hence wanted to know though the output of DateDiff is a integer number but using the integer within quotes also does not fail the result ?

    thanks

    Dhananjay

  • The explanation of described behavior is within article about Data Type Precedence: http://msdn.microsoft.com/library/ms190309.aspx

    "When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence."

    In your case:

    datediff (day, ....) > '0'

    integer value returned by datediff operator is compared to string expression.

    String has lower type precedence than integer, that leads to conversion to integer type and results returned by this comparison is equal to result of comparison datediff (day, ...) > 0

  • Thanks so much for the help to get me understand this since I come from Java world...

    Also needed some help to know which is a best book to learn TSQL from SSRS reporting needs? since my new job is SSRS reporting - i.e. creating reports using TSAL in MSSQL2008 and then using them in Visual Studio to create repprts and post them on SSRS web...

    Thanks

    Dhananjay

  • dhananjay.nagarkar (3/12/2013)


    Thanks so much for the help to get me understand this since I come from Java world...

    Also needed some help to know which is a best book to learn TSQL from SSRS reporting needs? since my new job is SSRS reporting - i.e. creating reports using TSAL in MSSQL2008 and then using them in Visual Studio to create repprts and post them on SSRS web...

    Thanks

    Dhananjay

    Not sure about the books, but there is a Stairway Series in SSC which might be helpful to you

    http://www.sqlservercentral.com/stairway/72382/


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

Viewing 4 posts - 1 through 3 (of 3 total)

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