DATEPART Function

  • Why does this returns 59 seconds ?

    SELECT DATEPART(SS,CAST('2012-05-06 11:07:09.313' AS DATETIME) - CAST('2012-05-06 11:07:09.317' AS DATETIME))

    Thanks

  • your query's subtraction basically generating 1899-12-31 23:59:59.997 date and DATEPART function picking up the second's value which is 59

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Check the results of this query:

    select CAST('2012-05-06 11:07:09.313' AS DATETIME) - CAST('2012-05-06 11:07:09.317' AS DATETIME)

    The result of this query is - 1899-12-31 23:59:59.997. I have to admit that I don't really understand what you are trying to do when you subtract datetime from datetime. From you question it seems that you don't understand why you got the results that you got. If you'll write what you are trying to do, there is a good chance that you'll get help.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Thanks. Basically, i was trying to findout the difference in seconds between 2 given dates.

  • moorthy.jy (5/7/2012)


    Thanks. Basically, i was trying to findout the difference in seconds between 2 given dates.

    In that case, you should use the function datediff.

    SELECT datediff(SS,'2012-05-06 11:07:09.313', '2012-05-06 11:07:09.317')

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Thanks again Adi. I was bit confused with the datepart function

Viewing 6 posts - 1 through 5 (of 5 total)

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