Isnull () function test)

Source: Internet
Author: User

From: http://www.cnblogs.com/wghao/archive/2009/11/22/1608285.html

 

Use test
Go
/* Isnull () function test

Isnull (check_expression, replacement_value)
*/

1. The data type is the same, but the length is different.

 

Declare @ x nvarchar (5 ),
@ Y nvarchar (50)

Set @ Y = n' 123456'
Print isnull (@ X, @ Y) -- @ Y is intercepted, and "12345" is returned"

Go

 

2. Different data types (nvarchar-> INT)

 

Declare @ x int,
@ Y nvarchar (50)

Set @ Y = n' 123456'
Print isnull (@ X, @ Y) + n'1' -- convert it to the int data type and add "1". Therefore, "123457" is returned"

Go

 

3. Different data types (INT-> nvarchar)

 

Declare @ x nvarchar (50 ),
@ Y int

Set @ Y = n' 123456'
Print isnull (@ X, @ Y) + n'1' -- convert it to the nvarchar data type first, and then add "1", so "1234561" is returned"
Go

 

4. Different data types (nvarchar-> uniqueidentifier)

 

Declare @ x uniqueidentifier,
@ Y nvarchar (50)

Set @ Y = n' 123456'
Print isnull (@ X, @ Y) -- receive the error message: "failed to convert string to uniqueidentifier ".
Go

 

5. An interesting test (nvarchar --> null)

 

Declare @ Y nvarchar (50)

Set @ Y = n' 123456'
Print isnull (null, @ Y) + n'1' -- indicates that when check_expression is null, the returned data type is replacement_value, so the nvarchar type of @ Y is returned: "1234561"

 

6. An interesting test (int --> null) -- to verify the 5th examples.

 

Declare @ Y int

Set @ Y = n' 123456'
Print isnull (null, @ Y) + n'1' -- indicates that when check_expression is null, the returned data type is replacement_value. Therefore, the int type of @ Y is returned: "123457"
Go

 

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.