The isnull problem in SQL.

Source: Internet
Author: User

Today when writing SQL code to write a ISNULL (variable 1, variable 2), the return of the result is actually "*", the asterisk, depressed for a long time.

The code has the following effect:

Declare @str1 varchar (1) Declare @str2 int Set @str2 = 222 Select ISNULL (@str1,@str2)

Return the result: "*", this depressed ah.

Modify the following code:

Declare @str1 varchar (4) Declare @str2 int Set @str2 = 222 Select ISNULL (@str1,@str2)

Returns the result: "222".

Modify the code again:

Declare @str1 varchar (4) Declare @str2 varchar (8) Set @str2 = ' 2222222 ' Select ISNULL (@str1,@str2)

Returns the result: "2222".

After these three test results: guessing the type of result returned by IsNull is closely related to the first variable, even depending on the type of the first variable.

Official explanation:

Syntax
(Check_,)
Arguments
Check_expression

Is the expression to being checked for NULL. check_expression can is of any type.

Replacement_value

is, the expression to being returned if check_expression is NULL. Replacement_value must be of a type, that's implicitly convertible to the type of check_expresssion.

Return Types

Returns the same type as check_expression.

Remarks

The value of check_expression is returned if it was not NULL; Otherwise, Replacement_value was returned after it was implicitly converted to the type of check_expression , if the types is different.

The return value is the second argument, which turns to the type of the first argument, the conversion fails, the exception is reported, or the preceding "*", the asterisk, appears.

The isnull problem in SQL.

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.