2014-08-27 60 views
1

我試圖將此查詢從Microsoft訪問轉換爲SQL Server 2012將訪問SQL轉換爲SQL服務器

SELECT 
IIF(IsNull(Letter), '', CalculationType) AS [BF NI Calculation Basis] 
FROM NICs 

我把它轉換成follwing,但我不斷收到錯誤:

Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value 'A' to data type int.

這是我曾嘗試:

SELECT 
CHOOSE(ISNULL(Letter,0), '', CalculationType) AS [BF NI Calculation Basis] 
FROM NICs 

SELECT 
IIF(Letter = 0, '', CalculationType) AS [BF NI Calculation Basis] 
FROM NICs 
+0

CalculationType的數據類型是什麼? – 2014-08-27 08:45:53

+0

@KrishnrajRana它是詮釋 – user3734454 2014-08-27 08:47:15

+0

檢查我的答案... – 2014-08-27 09:00:10

回答

1

如果你想檢查是否書比寫NULL:如果您想查詢函值0

SELECT 
IIF(Letter ='0', '', CalculationType) AS [BF NI Calculation Basis] 
FROM NICs 
0

這是你需要的嗎?

SELECT 
CASE 
WHEN (ISNULL(Letter,0)) = '' THEN CalculationType ELSE ISNULL(Letter,0) 
END AS [BF NI Calculation Basis] 
FROM NICs 

看看這個:

create table #NICs 
(
    Letter int, 
    CalculationType int 
) 

insert into #NICs values (1,100) 
insert into #NICs values (null,200) 
insert into #NICs values ('',300) 


SELECT 
CASE 
WHEN (ISNULL(Letter,0)) = '' THEN CalculationType ELSE ISNULL(Letter,0) 
END AS [BF NI Calculation Basis] 
FROM #NICs 

drop table #NICs 

編輯:

SELECT 
CASE 
WHEN (ISNULL(try_parse(Letter as int),0)) = '' THEN CalculationType ELSE ISNULL(try_parse(Letter as int),0) 
END AS [BF NI Calculation Basis] 
FROM #NICs 
+0

我仍然得到相同的錯誤消息245,當我嘗試這個。 – user3734454 2014-08-27 08:51:06

+0

編輯。嘗試這個。 – sdrzymala 2014-08-27 08:57:25

1

根據這一MSDN link for IIF

返回從類型最高優先級的數據類型true_valuefalse_value。它意味着在你的查詢中true_value是''(字符串)和false_value是CalculationType(這是int),這就是你得到這個錯誤的原因。

所以你的查詢應該是這樣的。

SELECT 
IIF(Letter = '0', 0, CalculationType) AS [BF NI Calculation Basis] 
FROM NICs 
1

我認爲這是查詢

SELECT 
IIF(Letter IS NULL, '', CalculationType) AS [BF NI Calculation Basis] 
FROM NICs 

你正在尋找:

SELECT CASE 
     WHEN Letter IS NULL THEN '' 
     ELSE CAST(CalculationType AS NVARCHAR(MAX) 
    END AS [BF NI Calculation Basis] 
FROM NICs 

這是一個解決方案無線列[BF NI計算基礎]作爲NVARCHAR。

希望這會有所幫助。

相關問題