SQL Server and implicit conversion of types

前端 未结 2 1663
傲寒
傲寒 2020-12-09 23:17

Where does SQL Server do implicit conversion and what are the rules it follows? I.E when does it convert the left side of the equality operator over the right side?

相关标签:
2条回答
  • 2020-12-09 23:57

    This is the list you are after DataType Precedence

    In your examples:

    WHERE quantity > '3'
    

    '3' is cast to int, matching quantity

    WHERE quantityTest > 3
    

    No casting required

    WHERE date = 20120101
    

    20120101 as a number is being cast to a date, which is too large. e.g.

    select cast(20120101 as datetime)
    

    This is different from

    WHERE date = '20120101'
    

    Where the date as a string can be cast.

    If you go down a third of the CAST and CONVERT reference to the section Implicit Conversions, there is a table of implicit conversions that are allowed. Just because it is allowed doesn't mean it will work, such as (20120101 -> datetime).

    0 讨论(0)
  • 2020-12-10 00:04

    There is a chart here on MSDN (also shown below) which shows all explicit and implicit data type conversions that are allowed for SQL Server system-supplied data types. Also it explains the conversions which are NOT allowed etc.

    It explains

    -Explicit Conversions
    -Implicit Conversions
    -Conversions not allowed
    ...
    

    0 讨论(0)
提交回复
热议问题