2012-08-09 25 views
0

我正在嘗試這樣做,但我不確定它是否可行。我們可以使用不同的數據類型將列上的數據複製到另一個列上嗎?

我想使用convert函數複製另一個表的一個tblRecords1(nvarchar) of table1 to tblRecords(bigint)的數據。

但它給錯誤。有沒有可能這樣做?

確保tblRecords1中的值都是數值。

更新

我這樣做如下:

INSERT INTO tblRecords (current_job_title,Highest_Education_Stream) SELECT convert(bigint,current_job_title),convert(bigint,Highest_Education_Stream) FROM tblRecords1

錯了我是做什麼的?

更新 我忘了空值。他們正在創造問題。所以我完成了如下:

INSERT INTO tblRecords (current_job_title,Highest_Education_Stream) 

SELECT current_job_title = case when current_job_title is null then 0 else convert(bigint,current_job_title) end, 
       Highest_Education_Stream=case when Highest_Education_Streamis null then 0 else convert(bigint,current_job_title) end, 
FROM tblRecords1 
+1

顯示你的代碼,並告訴我們你是什麼錯誤獲得。但是,這是可能的。 – automatic 2012-08-09 11:18:55

回答

0

做以下

INSERT給出INTO tblRecords(current_job_title,Highest_Education_Stream) SELECT CAST(BIGINT爲 「current_job_title的數據類型」) ,cast(bigint as「Highest_Education_Stream的數據類型」)FROM tblRecords1

替換實際數據類型 和「Highest_Education_Stream的數據類型」「current_job_title的數據類型」和空檢查使用

ISNULL(可變,0)

0

您可以轉換,提供您正在轉換的類型可以明智地匹配它正在轉換的類型。所以,你可以這樣做:

declare @from varchar(20) = '1000000' 
declare @to bigint 

select @to = convert(bigint, @from) 
select @to 

CAST and CONVERT on MSDN

相關問題