What will happen?

  • Dave62 (9/1/2010)


    SELECT DATEADD(day, DATEDIFF(day, '20100101', CURRENT_TIMESTAMP), '20100101');

    The only thing I don't like about this method is the hard-coded string. I suppose a dynamic expression could replace it but it would start to become a little tedious.

    I don't really see the hard-coded string as a problem. The only requirement is that you use the same date twice, it doesn't matter what date you choose. There is no risk of overflow, as there are way more integers in the int domain, then days in the datetime domain.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • webrunner (9/1/2010)


    DECLARE @a INT

    DECLARE @b-2 DATETIME

    SET @a = @b-2

    The above code executed successfully for me in SQL 2005 but gave the error listed in the answer when I ran it in SQL 2000**. So I think it is dependent on the SQL version, at least in the form expressed above.

    Which version of SQL Server 2005? When I run it, I get "Msg 257, Level 16, State 3, Line 3

    Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query."

    (I am using Microsoft SQL Server 2005 - 9.00.4053.00)


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (9/1/2010)


    ...

    I don't really see the hard-coded string as a problem. The only requirement is that you use the same date twice, it doesn't matter what date you choose. There is no risk of overflow, as there are way more integers in the int domain, then days in the datetime domain.

    You're right Hugo. I guess we have 2 ways to get the same results without the integer addition.

    But one way is a little more concise with a little less hard-coding. 😉

  • Dave62 (9/1/2010)


    SELECT DATEADD(day, DATEDIFF(day, '20100101', CURRENT_TIMESTAMP), '20100101');

    The only thing I don't like about this method is the hard-coded string. I suppose a dynamic expression could replace it but it would start to become a little tedious.

    I don't mind having 2 converts in there because mbova407's original example had 2. This method below will return the same result and datatype as mbova407's without the need for the integer addition.

    Select Convert(Datetime, CONVERT(varChar(10), getDate(), 101));

    The latter method is considerably more expensive though. The SQL Musings from the Desert article by Lynn Pettis has been dissected by quite few posts and from what I remember, the verdict is pretty clear: dateadd and datediff combination is far cheaper than conversion functions. Hugo's script looks somewhat unusual because it uses some varchar value which is guaranteed to be dateformat independent valid datetime value (YYYYMMDD is always translated correctly regardless of local format). What is usually used instead is 0, because it is simply zero date, shorter to type and much faster to convert (rather than rely on engine's ability to convert varchar to datetime), i.e.

    select dateadd(day, datediff(day, 0, current_timestamp), 0);

    Why faster is because how datetime is stored internally (4 bytes for number of days from zero date and 4 bytes for number of ticks from midnight of today).

    <!-- Begin blatant self promotion

    One of my answers on the ask side has in-depth explanation of datetime internals, and according to Matt Whitfield, it "sounds spot on". Here is the link: http://ask.sqlservercentral.com/questions/16420/php-with-mssql-strtotime-with-mssql-datetime-column

    End blatant self promotion -->

    Oleg

  • Hugo Kornelis (9/1/2010)


    webrunner (9/1/2010)


    DECLARE @a INT

    DECLARE @b-2 DATETIME

    SET @a = @b-2

    The above code executed successfully for me in SQL 2005 but gave the error listed in the answer when I ran it in SQL 2000**. So I think it is dependent on the SQL version, at least in the form expressed above.

    Which version of SQL Server 2005? When I run it, I get "Msg 257, Level 16, State 3, Line 3

    Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query."

    (I am using Microsoft SQL Server 2005 - 9.00.4053.00)

    Sorry, my mistake. (BTW, My SQL 2005 is version 9.0.3080.)

    This code runs OK in both versions:

    DECLARE @a INT

    DECLARE @b-2 DATETIME

    SET @b-2 = @a

    This code throws an error in both versions:

    DECLARE @a INT

    DECLARE @b-2 DATETIME

    SET @a = @b-2

    I mixed up the "SET @a = @b-2" version with the "SET @b-2= @a" version in my earlier post. I seem to have run one version in SQL 2000 and the other in SQL 2005.

    Attention to detail, anyone? :hehe:

    Thanks,

    webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Oleg Netchaev (9/1/2010)


    Hugo's script looks somewhat unusual because it uses some varchar value which is guaranteed to be dateformat independent valid datetime value (YYYYMMDD is always translated correctly regardless of local format). What is usually used instead is 0, because it is simply zero date, shorter to type and much faster to convert (rather than rely on engine's ability to convert varchar to datetime), i.e.

    select dateadd(day, datediff(day, 0, current_timestamp), 0);

    Why faster is because how datetime is stored internally (4 bytes for number of days from zero date and 4 bytes for number of ticks from midnight of today).

    Correct. Thanks, Oleg!

    The reason I use a date in the 'yyyymmdd' format instead of an integer value is because it's easier for the eye (assuming an eye that is somewhat used to this format) to interpret as a date. As a matter of principle, I always use 'yyyymmdd' for date constants in my code. I would never dream of using the integer constant 40421 to represent Sep 1st, 2010 (and I hope noone would even consider that), even though it does indeed convert to datetime faster than the string constant '20100901'. And I don't like to make an exception for representing the date Jan 1st, 1900.

    But I do agree that it's quite common to use 0 in this particular construction, and that it does not really hurt.:-)


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (9/1/2010)


    Oleg Netchaev (9/1/2010)


    ...

    select dateadd(day, datediff(day, 0, current_timestamp), 0);

    Why faster is because how datetime is stored internally (4 bytes for number of days from zero date and 4 bytes for number of ticks from midnight of today).

    The reason I use a date in the 'yyyymmdd' format instead of an integer value is because it's easier for the eye (assuming an eye that is somewhat used to this format) to interpret as a date. As a matter of principle, I always use 'yyyymmdd' for date constants in my code. I would never dream of using the integer constant 40421 to represent Sep 1st, 2010 (and I hope noone would even consider that), even though it does indeed convert to datetime faster than the string constant '20100901'. And I don't like to make an exception for representing the date Jan 1st, 1900.

    But I do agree that it's quite common to use 0 in this particular construction, and that it does not really hurt.:-)

    Thank you Oleg & Hugo. I was not surprised by the question (not to imply anything about the question itself) as I have faced these conversion issues earlier but was looking at the discussion anyways and found this interesting piece of information.

  • Thanks for the question!

  • It should work

    SELECT COVERT(VARCHAR,GETDATE(),111);

  • Robert Dennyson (9/2/2010)


    It should work

    SELECT COVERT(VARCHAR,GETDATE(),111);

    If you add the missing letter N (CONVERT), it does work. It is an explicit conversion from datetime to varchar format, using the yyyy/mm/dd format (a format that is not locale-neutral and hence not recommended in various places, but if you need the yyyy/mm/dd format for a report, this is the function to use).

    However, I fail to see the relation with the question discussed here, which is about implicit conversion from datetime to int, not about explicit conversion from datetime to varchar.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • good question. some good discussion too. but shifting from explicit conversion of datetime to int (fast and efficient) to explicit conversion of datetime to a very local varchar date format seemed a little bizarre.

    Tom

  • Good and straight forward question. Thanks

Viewing 12 posts - 16 through 26 (of 26 total)

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