SQL中ISNULL的問題。

來源:互聯網
上載者:User

標籤:

今天在寫SQL代碼的時候寫了個 ISNULL(變數1,變數2),返回的結果居然是 "*" ,這個星號,鬱悶了很久。

代碼大意如下:

declare @str1 varchar(1)declare @str2 intset @str2=222select ISNULL(@str1,@str2)

返回結果:" * ",這個鬱悶啊。

修改下代碼:

declare @str1 varchar(4)declare @str2 intset @str2=222select ISNULL(@str1,@str2)

返回結果: " 222 " 。

再次修改代碼:

declare @str1 varchar(4)declare @str2 varchar(8)set @str2=‘2222222‘select ISNULL(@str1,@str2)

返回結果:"  2222 "。

經過這三次的實驗結果:猜測isnull返回的結果類型,與第一個變數是息息相關的,甚至就取決於第一個變數的類型。

 

官方解釋:

Syntax
 
ISNULL ( check_expression , replacement_value )
Arguments
check_expression

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

replacement_value

Is the expression to be returned if check_expression is NULL. replacement_value must be of a type that is 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 is not NULL; otherwise, replacement_value is returned after it is implicitly converted to the type of check_expression, if the types are different.

傳回值是第二個參數,會轉成第一個參數的類型,轉換失敗,就會報異常或者出現前面的"*",星號。

 

SQL中ISNULL的問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.