简体   繁体   English

将 Oracle TIMESTAMP(6) 转换为 SQL SERVER 2008 DATETIME2(6)

[英]Converting Oracle TIMESTAMP(6) TO SQL SERVER 2008 DATETIME2(6)

I am bulk importing a csv file to SQL server 2008, the csv file has been generated from exporting the table data from Oracle SQL developer.我正在将 csv 文件批量导入到 SQL Server 2008,该 csv 文件是通过从 Oracle SQL 开发人员导出表数据生成的。 The data for one column in that csv file is in TIMESTAMP(6) for which I am having the DATETIME2(6) datatype for the required column in the SQL server 2008. I am importing the CSV file using the below statement该 csv 文件中一列的数据在 TIMESTAMP(6) 中,我在 SQL server 2008 中拥有所需列的 DATETIME2(6) 数据类型。我正在使用以下语句导入 CSV 文件

USE H_CLAIMS
GO
BULK INSERT H_CLAIMS.dbo.APPLICATION_QUEUES
FROM 'D:\MyWork\HC DB Work\HCAIDDB_CSV_EXPORTS\APPLICATION_QUEUES_export.CSV'

WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\\n') GO WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\\n') 去

while doing above I am getting the below error Msg 4864, Level 16, State 1, Line 1 Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 5 (CREATED_DATE).在执行上述操作时,我收到第 1 行第 5 列(CREATED_DATE)的以下错误消息:消息 4864,级别 16,状态 1,第 1 行批量加载数据转换错误(指定代码页的类型不匹配或无效字符)。 Msg 4864, Level 16, State 1, Line 1消息 4864,级别 16,状态 1,第 1 行

The sample data in the column mentioned in the error is like 21-NOV-14 08.57.51.565214000 AM错误中提到的列中的样本数据类似于 21-NOV-14 08.57.51.565214000 AM

So I am looking for the answer, which can overcome this issue with any other attributes during the bulk insert statement or any convert function which can properly convert the datetime in the sample data to SQL SERVER 2008 datetime2 format.所以我正在寻找答案,它可以在批量插入语句期间使用任何其他属性或任何可以将示例数据中的日期时间正确转换为 SQL SERVER 2008 datetime2 格式的转换函数来克服这个问题。

SQL Server doesn't know how to convert the text value " 21-NOV-14 08.57.51.565214000 AM" to a DATETIME2 column. SQL Server 不知道如何将文本值“21-NOV-14 08.57.51.565214000 AM”转换为 DATETIME2 列。 Try it in a query analyser window :在查询分析器窗口中尝试:

SELECT CAST('21-NOV-14 08.57.51.565214000 AM' AS DATETIME2(6))

Note that if you're using DATETIME2(6) it'll be loosing precision compared to what you're trying to import.请注意,如果您使用 DATETIME2(6),与您尝试导入的内容相比,它会失去精度。 Have a look at http://msdn.microsoft.com/en-GB/library/bb677335.aspx .看看http://msdn.microsoft.com/en-GB/library/bb677335.aspx

When I've had to do this coming from DB2 text files, I've done it two different ways.当我不得不通过 DB2 文本文件来做这件事时,我用了两种不同的方式。

  1. Import the datetime field into a varchar then written a bit of SQL to manipulate the string into a format SQL Server can recognise, so something like.将日期时间字段导入 varchar,然后编写一些 SQL 来将字符串操作为 SQL Server 可以识别的格式,例如。 Bit slow and clunky, especially if you have a lot of data.有点慢和笨重,特别是如果你有很多数据。
  2. Use SSIS and create a transformation to do the string manipulation there.使用 SSIS 并创建一个转换来在那里进行字符串操作。 This has the advantage of still being able to bulk insert into the destination table, but does mean you need to be able to have access to integration services.这具有仍然能够批量插入目标表的优势,但确实意味着您需要能够访问集成服务。

As I couldn't find any bulk Insert which will do the work for me, I have gone with a different approach.由于我找不到任何可以为我完成工作的批量插入,因此我采用了不同的方法。 After many trails with cast and convert, I followed the below approach which is working as expected I have created a function which can convert the oracle timestamp(6) to nvarchar of sql which can be directly inserted as datetime2(6) datatype in sql server 2008. Below is the function Then I have used a stored procedure which can accept the file path as input parameter and a temp table to hold the nvarchar based datetime2 value.在进行了多次转换和转换之后,我遵循了以下方法,该方法按预期工作2008. 下面是函数然后我使用了一个存储过程,它可以接受文件路径作为输入参数和一个临时表来保存基于 nvarchar 的 datetime2 值。 In the stored procedure I have used the dynamic bulk insert statement to insert into the required table.在存储过程中,我使用动态批量插入语句插入到所需的表中。 The procedure is after the function程序在函数之后

CREATE FUNCTION DATETIMECONVERTER
(
@ORACLETIMESTAMP NVARCHAR(100) 
)RETURNS nvarchar(100)
AS 
BEGIN
DECLARE @convertedString nvarchar(100); 
select @convertedString= replace(@ORACLETIMESTAMP,'.',':');
RETURN STUFF(@convertedString, CHARINDEX(':', @convertedString,18), 1, '.')
END
GO


CREATE PROCEDURE IMPORT_APPLICATION_ROLES @PATH varchar(1000)
AS
IF OBJECT_ID('H_CLAIMS.DBO.TEMP_APPLICATION_QUEUES', 'U') IS NOT NULL
DROP TABLE H_CLAIMS.DBO.TEMP_APPLICATION_ROLES
CREATE TABLE H_CLAIMS.DBO.TEMP_APPLICATION_ROLES
(
ROLE_ID INT NOT NULL, 
ROLE_NAME NVARCHAR(255), 
ROLE_DESC NVARCHAR(255), 
CREATED_BY NVARCHAR(100), 
CREATED_DATE NVARCHAR(100), 
UPDATED_BY NVARCHAR(100), 
UPDATED_DATE NVARCHAR(100)
)

DECLARE @bulkInsert NVARCHAR(4000) = 'BULK INSERT TEMP_APPLICATION_ROLES FROM ''' + @PATH + ''' WITH ( FIELDTERMINATOR ='','', ROWTERMINATOR =''\n'' )';
EXEC(@bulkInsert)

INSERT INTO APPLICATION_ROLES
(ROLE_ID,ROLE_NAME,ROLE_DESC,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE)
SELECT ROLE_ID,ROLE_NAME,ROLE_DESC,CREATED_BY,dbo.DATETIMECONVERTER(CREATED_DATE)AS CREATED_DATE,
UPDATED_BY,dbo.DATETIMECONVERTER(UPDATED_DATE) AS UPDATED_DATE
FROM H_CLAIMS.dbo.TEMP_APPLICATION_ROLES

DROP TABLE H_CLAIMS.DBO.TEMP_APPLICATION_QUEUES
GO

to execute the statment I have used the below statement执行语句我使用了以下语句

EXEC H_CLAIMS.DBO.IMPORT_APPLICATION_QUEUES @PATH='D:\my_export.CSV';

确保在执行存储过程时将 .csv 文件放在服务器机器驱动器中

I may be late to answer this but allow me to give you my workaround (if the precision doesn't really matter) I import the timestamp from oracle table into SQL 2008 varchar then I update the varchar into a format that will fit for datetime2 then I alter the SQL table column to data type datetime2.我可能会迟到回答这个问题,但请允许我给你我的解决方法(如果精度并不重要)我将时间戳从 oracle 表导入 SQL 2008 varchar 然后我将 varchar 更新为适合 datetime2 的格式然后我将 SQL 表列更改为数据类型 datetime2。 EG: in case you have time stamp like '01-JAN-15 12.00.00.000000000 AM +05:30' EG:如果你有像'01-JAN-15 12.00.00.000000000 AM +05:30'这样的时间戳

update My_Table 
set MyTimeStamp = 
substring(MyTimeStamp, 1,10)+
REPLACE(substring(MyTimeStamp, 11, 8),'.',':')+
substring(MyTimeStamp, 19, 13) 
where MyTimeStamp like '%.%.%.%';

alter table [My_Table] alter column MyTimeStamp DATETIME2; 
Go;

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

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