2017-03-07 51 views
1

我想檢查我們的MSSQL服務器的數據以驗證它是以數字(2,0)格式。我目前正在使用下面的方法,但想知道如何使用數字函數來檢查數據庫中現有數據的語法。使用數字()函數檢查SQL服務器數據格式

當前代碼或方法,我現在用:

(Case when @variable is not NULL 
and @variable like '%.%' 
    and blahblah =blahblah 
    then 0 else 1 end) as @Variable, 

感謝您的幫助。

+0

您使用的是哪個版本的SQL Server? – asemprini87

+0

我目前正在使用2014 –

+0

您是否嘗試過ISNUMERIC()函數? – asemprini87

回答

1

爲什麼要使用數字函數?要做到這一點,最好的辦法是使用try_convert()(可在SQL Server 2012+):

select (case when try_convert(numeric(2, 0), @variable)) is not null and 
        . . . 
      then 0 else 1 
     end) as <whatever> 

注:is not null意味着轉換成功。我不確定這是否是您真正想要的邏輯,也許您想要is null

+0

太棒了。謝謝!那工作 –