2014-01-15 33 views
0

我無法連接具有不同數據類型的兩列。將數據類型varchar轉換爲數字連接表時出錯

main_ref_idcharperson_idnumeric

查詢:

select 
    main_ref_id 
from 
    not 
where 
    main_ref_type = 'P' 
    and convert(numeric(9, 0), main_ref_id) in (select cast(person_id as numeric) 
               from person_wo_memberships) 

錯誤

消息8114,級別16,狀態5,行1個
誤差變換數據類型爲varchar到數字。

任何幫助?

+0

長度,你可以分享你的表結構,你也可以嘗試加入列而不轉換爲數字。只要加入他們作爲字符串。 – gokul

+1

您正在將'main_ref_id'轉換爲NUMERIC(9,0)和'person_id'爲NUMERIC(18,0)。您是否查看了這兩列中的數據以查看是否有非數字值?它們是什麼數據類型?而且我不相信'from from where'語法是合理的。任何其他龍丟失錯誤? – HABO

回答

1

您已經聲明main_ref_idchar,但您正試圖將其轉換爲numeric值。錯誤是不言自明的。

嘗試轉換person_id,這是numericchar

select main_ref_id 
from [not] 
where main_ref_type='P' and 
main_ref_id in 
    (select cast(person_id as varchar) 
     from person_wo_memberships) 

請確定在cast聲明varchar的長度,以匹配main_ref_id

相關問題