插入表时转换列的数据类型

时间:2012-09-08 11:08:59

标签: sql sql-server

我希望将列数据类型从'VarChar'更改为'DataType'从源表中插入另一个表

我可以在T-SQL

中完成
Declare @DateTime   As      DateTime
Declare @Date       As      NVARCHAR(20)

Set @Date = '120901'
Set @DateTime = CONVERT(Date,'20'+SUBSTRING(@Date,1,2)+'-'+SUBSTRING(@Date,3,2)+'-'+SUBSTRING(@Date,5,2))
Print @DateTime

如何在insert Statement

中实现它
Insert Into AxisReportMain 
Select (Column1,Column2,Column3)
From ReportMain

Column2包含Date为'VarChar'。在AxisReportMain表中Column2 DataType是“Date”

1 个答案:

答案 0 :(得分:1)

尝试:

Insert Into AxisReportMain 
Select Column1,
       CONVERT(Date,'20'+SUBSTRING(Column2,1,2)+'-'+SUBSTRING(Column2,3,2)+'-'+SUBSTRING(Column2,5,2)) as Column2,
       Column3
From ReportMain
Where Column2 <> '000000'