2013-10-07 51 views
0

我有以下功能,應該檢查數字是4位數。檢查位數

function f_checkNum(
     @pnum integer 
    ) returns integer 
    begin  
     return case 
       when @pnum like '[0-9][0-9][0-9][0-9]' then 1  
       else 0 
      end;  
    end 

此工作正常,如果輸入的數字是根據4位,但如果他們是在4提示錯誤

Msg 8115, Level 16, State 2, Line 1 
Arithmetic overflow error converting expression to data type int. 

請讓我知道如何解決它。由於

+1

多少超過4個位數? 5? –

+0

嘗試將pnum作爲varchar –

回答

2
Return CASE WHEN @pnum between 1000 and 9999 Then 1 Else 0 End 

如果您需要包括負數則

Return CASE WHEN ABS(@pnum) between 1000 and 9999 Then 1 Else 0 End 
0

嘗試這樣:

return case 
    when LEN(cast(@pnum as varchar)) = 4 then 1 
    else 0 
    end;  
0

您可以使用該功能來代替。這將檢查您的號碼的長度,並迎合負數。

create function f_checkNum(
    @pnum integer 
) returns integer 
begin 
return case when len(cast(@pnum as varchar(max))) = 4 and @pnum > 0 
    then 1 
    else 0 
    end 
end