简体   繁体   English

如何将数据从一个表复制到另一个表,其中列数据类型不同?

[英]How to copy data from one table to another where column data types are different?

I have two tables. 我有两张桌子。

  1. NEW [contains data] [all columns are varchar] NEW [包含数据] [所有列都是varchar]

  2. NEW2 [empty table] [columns are of different data types] NEW2 [空表] [列具有不同的数据类型]

I want to copy all data from New to New2. 我想将所有数据从New复制到New2。

What i did is, 我做的是,

SELECT T.* 
INTO   #tmp 
FROM   (SELECT * 
        FROM   [dbo].[new]) AS T 

then 然后

INSERT INTO New2(col1, col2....)
SELECT *
FROM #TMP

But its not working. 但它不起作用。

Msg 242, Level 16, State 3, Line 2
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
The statement has been terminated.

[what I want is to change the column data types of NEW table, especially the varchar to smalldatetime. [我想要的是更改NEW表的列数据类型,尤其是varchar到smalldatetime。 So I tried this way. 所以我试过这种方式。 Any other approach is also welcome.] 任何其他方法也是受欢迎的。]

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank You. 谢谢。

Yes. 是。 Done. 完成。

What I did is, Imported Excle data in SQL Server table with all columns in a table as varchar data type. 我所做的是,在SQL Server表中导入Excle数据,表中的所有列都是varchar数据类型。 The problem was in excel data, the date values, somewhere was NA. 问题出在excel数据,日期值,某处是NA。 So I had to replace all those NA values with null. 所以我不得不用null替换所有那些NA值。

To check for those invalid date values in a table, I used following command. 要检查表中的那些无效日期值,我使用了以下命令。

SELECT ISDATE(COL_NAME) AS Result

SELECT ISNULL(COL_NAME) AS Result

For this, sometime you have to also check & set for the date format of SQL Server using following commands, 为此,有时您还必须使用以下命令检查和设置SQL Server的日期格式,

DBCC useroptions
SET DATEFORMAT mdy

Then all the result values I replaced them with NULL as 然后我将所有结果值替换为NULL为

UPDATE TABLE SET COLUMN = NULL WHERE ISDATE(COLUMN) = 0 OR COLUMN = 'NA'

At last I updated required columns manually using simple alter commands as, 最后,我使用简单的alter命令手动更新了所需的列,

ALTER TABLE ALTER COLUMN COL_NAME <<data type>>

I also changed my dateforamat to dmy which prior was mdy. 我也将我的dateforamat改为dmy,之前是mdy。

Thank for Suraj Singh, Deepshikha for their helpful suggestions. 感谢Suraj Singh,Deepshikha提供了有用的建议。

While inserting cast your column to smalldatetime 插入时将列转换为smalldatetime

SET DATEFORMAT ymd 
INSERT INTO New2(col1, col2....)
SELECT Col1,Col2 , CAST('2007-05-08 12:35:29'  AS smalldatetime) As Col_Name,...Col3
FROM #TMP

Try as: 试着:

DECLARE @NEW TABLE([date] VARCHAR(20));

INSERT @NEW SELECT '2/8/2013 15:00' ;

select LEFT([date],2) + SUBSTRING([date],3,2) + SUBSTRING([date],5,4) + ' '+ RIGHT([date],5)+':00'
from @NEW 

UPDATE @NEW SET [date] = CONVERT(CHAR(16), CONVERT(SMALLDATETIME, 
                         LEFT([date],2) + SUBSTRING([date],3,2) + SUBSTRING([date],5,4) + ' '+ RIGHT([date],5)+':00', 120));

SELECT [date], CONVERT(SMALLDATETIME, [date]) FROM @NEW;

Try This 尝试这个

SET DATEFORMAT ymd 
INSERT INTO destination_table(column_name)
SELECT Column_name As Aliace_name
FROM Source_table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM