標籤:
今天在寫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的問題。