"Go" implicit conversion of SQL Server scalar expressions

Source: Internet
Author: User
Tags scalar

There is a priority problem in the data type in SQL Server. The return result type of a scalar expression is also determined by the type of operand, such as 1 + ' 1 ' =2. Instead of ' 11 ', the precedence of some int is higher than that of the varchar type. So the result of the expression is implicitly converted to the int type.

Also for scalar functions, such as one column of a table is an int, the table has two rows with values of 2 and 3 if you use the AVG function for this column, the result is 2, not 2. 5. But if this column is of type decimal, then the result is 2.5. Because the result type depends on the operational data type.

A case name like the one below

Case

When <logical_expression1> then <int_expression>

When <logical_expression2> then <varchar_expression>

When <logical_expression3> then <decimal_expression>

END

The type of the return value is the decimal type, even if the first expression satisfies a condition.

But

SELECT

Case

When 1 > 1 then 10

When 1 = 1 Then ' abc '

When 1 < 1 then 10.1

END

This will cause a syntax error,

You can then use the sql_variant type,

SELECT
Case
When 1 > 1 then CAST (sql_variant)
When 1 = 1 then CAST (' abc ' as sql_variant)
When 1 < 1 then CAST (10. As sql_variant)
END;

"Go" implicit conversion of SQL Server scalar expressions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.