• Open Minded (7/1/2010)


    I think MDAS is also followed when there are a bunch of operators between numbers. I guess 'select 6+2+-+7' became 1 happened in this manner:

    • . 6+2+-+7 -> start
    • 8-7 -> 7 and 2 are added to an implicit zero.
    • 1 -> finally, subtraction

    I think I could post a cute QOTD based on this, 'select 6+2+*-+7'?

    My first guess is 1 if implicit zeros exists. Now let me start my 2005 and run the select...Hmm...Interesting. I had to do 'select 6+2+0*0-+7'. So implicit zeros do not exist, it's just a thing for add and sub operators.

    Btw, I did not get the question right.

    You are mixing up two things. The implicit zero you mean is coming from a string converted to integer. Your select

    select 6+2+*-+7

    contains no strings.

    You can actually easily see what SQL Server converts to what (for single characters only):

    USE tempdb

    GO

    DROP PROCEDURE dbo.CheckConversion

    GO

    CREATE PROCEDURE dbo.CheckConversion AS

    DECLARE @i int

    SET @i = 0

    WHILE @i <= 65535

    BEGIN

    BEGIN TRY

    PRINT N'''' + NCHAR(@i) + N''' / NCHAR(' + CAST(@i as nvarchar(500)) + ') converts to ''' + CAST(CAST(NCHAR(@i) AS int) AS nvarchar(500)) + N''''

    END TRY

    BEGIN CATCH

    -- PRINT ERROR_MESSAGE()

    END CATCH

    SET @i = @i + 1

    END

    GO

    EXEC dbo.CheckConversion

    ' ' / NCHAR(32) converts to '0'

    '+' / NCHAR(43) converts to '0'

    '-' / NCHAR(45) converts to '0'

    '0' / NCHAR(48) converts to '0'

    '1' / NCHAR(49) converts to '1'

    '2' / NCHAR(50) converts to '2'

    '3' / NCHAR(51) converts to '3'

    '4' / NCHAR(52) converts to '4'

    '5' / NCHAR(53) converts to '5'

    '6' / NCHAR(54) converts to '6'

    '7' / NCHAR(55) converts to '7'

    '8' / NCHAR(56) converts to '8'

    '9' / NCHAR(57) converts to '9'

    If you convert to money you get weird results btw:

    '' / NCHAR(9) converts to '0.00'

    '

    ' / NCHAR(10) converts to '0.00'

    '' / NCHAR(11) converts to '0.00'

    '' / NCHAR(12) converts to '0.00'

    '

    ' / NCHAR(13) converts to '0.00'

    ' ' / NCHAR(32) converts to '0.00'

    '$' / NCHAR(36) converts to '0.00'

    '+' / NCHAR(43) converts to '0.00'

    ',' / NCHAR(44) converts to '0.00'

    '-' / NCHAR(45) converts to '0.00'

    '.' / NCHAR(46) converts to '0.00'

    '0' / NCHAR(48) converts to '0.00'

    '1' / NCHAR(49) converts to '1.00'

    '2' / NCHAR(50) converts to '2.00'

    '3' / NCHAR(51) converts to '3.00'

    '4' / NCHAR(52) converts to '4.00'

    '5' / NCHAR(53) converts to '5.00'

    '6' / NCHAR(54) converts to '6.00'

    '7' / NCHAR(55) converts to '7.00'

    '8' / NCHAR(56) converts to '8.00'

    '9' / NCHAR(57) converts to '9.00'

    '\' / NCHAR(92) converts to '0.00'

    ' ' / NCHAR(160) converts to '0.00'

    '¢' / NCHAR(162) converts to '0.00'

    '£' / NCHAR(163) converts to '0.00'

    '¤' / NCHAR(164) converts to '0.00'

    '¥' / NCHAR(165) converts to '0.00'

    '?' / NCHAR(2546) converts to '0.00'

    '?' / NCHAR(2547) converts to '0.00'

    '?' / NCHAR(3647) converts to '0.00'

    '?' / NCHAR(5760) converts to '0.00'

    '?' / NCHAR(6107) converts to '0.00'

    ' ' / NCHAR(8192) converts to '0.00'

    ' ' / NCHAR(8193) converts to '0.00'

    ' ' / NCHAR(8194) converts to '0.00'

    ' ' / NCHAR(8195) converts to '0.00'

    ' ' / NCHAR(8196) converts to '0.00'

    ' ' / NCHAR(8197) converts to '0.00'

    ' ' / NCHAR(8198) converts to '0.00'

    '?' / NCHAR(8199) converts to '0.00'

    '?' / NCHAR(8200) converts to '0.00'

    '?' / NCHAR(8201) converts to '0.00'

    '?' / NCHAR(8202) converts to '0.00'

    '?' / NCHAR(8203) converts to '0.00'

    '?' / NCHAR(8232) converts to '0.00'

    '?' / NCHAR(8233) converts to '0.00'

    '?' / NCHAR(8239) converts to '0.00'

    '?' / NCHAR(8352) converts to '0.00'

    '¢' / NCHAR(8353) converts to '0.00'

    '?' / NCHAR(8354) converts to '0.00'

    '?' / NCHAR(8355) converts to '0.00'

    '£' / NCHAR(8356) converts to '0.00'

    '?' / NCHAR(8357) converts to '0.00'

    '?' / NCHAR(8358) converts to '0.00'

    'P' / NCHAR(8359) converts to '0.00'

    '?' / NCHAR(8360) converts to '0.00'

    '?' / NCHAR(8361) converts to '0.00'

    '?' / NCHAR(8362) converts to '0.00'

    '?' / NCHAR(8363) converts to '0.00'

    '€' / NCHAR(8364) converts to '0.00'

    '?' / NCHAR(8365) converts to '0.00'

    '?' / NCHAR(8366) converts to '0.00'

    '?' / NCHAR(8367) converts to '0.00'

    '?' / NCHAR(8368) converts to '0.00'

    '?' / NCHAR(8369) converts to '0.00'

    ' ' / NCHAR(12288) converts to '0.00'

    '?' / NCHAR(65020) converts to '0.00'

    '?' / NCHAR(65129) converts to '0.00'

    '$' / NCHAR(65284) converts to '0.00'

    '?' / NCHAR(65504) converts to '0.00'

    '?' / NCHAR(65505) converts to '0.00'

    '?' / NCHAR(65509) converts to '0.00'

    '?' / NCHAR(65510) converts to '0.00'

    Best Regards,

    Chris Büttner