SSIS到SQL Server数据导入和转换问题

时间:2022-01-11 16:56:41

I am a beginner with SQL Server 2014. I need some advise with data type conversion from SSIS to SQL Server.

我是SQL Server 2014的初学者。我需要一些建议,从S​​SIS到SQL Server的数据类型转换。

I am working with a DATETIME column, an example value is "2017-08-17 16:00:50" which is in format 'YYYYMMDDHHMMSSsss'

我正在使用DATETIME列,示例值为“2017-08-17 16:00:50”,格式为'YYYYMMDDHHMMSSsss'

From SSIS I have used data type Unicode string [DT_WSTR] and in SQL Server I have used data type NVARCHAR.

从SSIS我使用了数据类型Unicode字符串[DT_WSTR],在SQL Server中我使用了数据类型NVARCHAR。

When I am trying to import the data from SSIS, it changes the date format from 2017-08-17 16:00:50 to 17/08/2017 4:00:50 PM. How do I prevent this and also when I try and convert the varchar date to datetime format using a cast or convert like this:

当我尝试从SSIS导入数据时,它会将日期格式从2017-08-17 16:00:50更改为2017年8月17日下午4:00:50。我如何防止这种情况以及当我尝试使用类似的转换或转换将varchar日期转换为datetime格式时:

With convert

随着转换

select 
    convert(datetime, [LastUpdateDateTime], 109) as [LastUpdateDateTime]
from 
    mytable

I get the this error:

我收到这个错误:

Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.

消息241,级别16,状态1,行2转换在从字符串转换日期和/或时间时失败。

With a cast

有演员

select 
    cast([LastUpdateDateTime] as datetime) as [LastUpdateDateTime]
from 
    mytable

I get the this error:

我收到这个错误:

Msg 242, Level 16, State 3, Line 2
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.

消息242,级别16,状态3,行2将nvarchar数据类型转换为日期时间数据类型导致超出范围的值。

Please advise,

请指教,

1 个解决方案

#1


0  

You should use date style 103 in convert
try this:

你应该在转换中使用日期样式103试试这个:

Declare @dt nvarchar(50)=CONVERT(nvarchar(50),[LastUpdateDateTime],103)
SELECT CONVERT(DATETIME2(0), @dt, 103)

#1


0  

You should use date style 103 in convert
try this:

你应该在转换中使用日期样式103试试这个:

Declare @dt nvarchar(50)=CONVERT(nvarchar(50),[LastUpdateDateTime],103)
SELECT CONVERT(DATETIME2(0), @dt, 103)