A real dates wtf.

  • A little bug has been bothering me all afternoon... I finally nailed it down to some interesting behaviour... the truncation of the ms part of the datetime!

    Running SQL 2kE, sp3.

    Can someone have a look on sp3a/sp4 and let me know if the behaviour is the same!?  Or if you get different behaviour on any sp...

    declare @a varchar(40),

     @b datetime

    set @a = '23 Sep 2005 16:23:58:650'

    select @a

    select @b-2 = @a

    select @b-2

    select @a = @b-2

    select @a

    select @b-2 = @a

    select @a

    Results:

    23 Sep 2005 16:23:58:650

    2005-09-23 16:23:58.650

    Sep 23 2005  4:23PM

    Sep 23 2005  4:23PM

     

     

  • This is because you rely on implicit conversion between varchar and datetime

    by default convert/cast use the mon dd yyyy hh:miAM format

    when casting datetime to varchar

  • Yes, I see.

    I fail to understand why the default behaviour should alter the underlying data so dramatically tho (cutting off both seconds and milliseconds)?

    Surely the sensible default would be the so called default + milliseconds (109).  Do you know of any way to alter the default behaviour?

    I wonder how many man hours get wasted by this kind of implicit conversion problem.

     

     

     

  • You should not rely in "Default" behaviours for data manipulation instead use type casting to get a desired behaviour (it is good to feel you are in control)

     


    * Noel

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

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