datetime – 在SSIS中将日期转换为字符串,当前名称是月份?

我收到错误,

Error converting data type varchar to date.

从这个表达

(DT_STR,30,1252)@[User::ToDate]

DO NET Source [40]: An error occurred executing the provided SQL command: “EXEC [dbo.StoredProcedure] @ToDate = ’01-maj-2018′

Error: 0xC004706B at Import (See Expression!), SSIS.Pipeline: “ADO NET Source” failed validation and returned validation status “VS_ISBROKEN”.

@ToDate = '01-maj-2018'

如何更改它以便我的@ToDate将是01-may-2018而不是01-maj-2018?

我试图搜索并且有许多类似的问题,但没有一个关于这个特定错误的问题.为什么它甚至使我的字符串有maj?

最佳答案 我同意@AlexanderVolok,看起来该软件包的LocalID与英语不同,或者在操作系统区域设置中选择的日期时间文化信息不同.

(1)使用条件更改月份名称? :

如果您可以更改这些属性,那么它可以解决问题,否则您在数据流任务之前添加表达式任务,使用条件运算符将日期转换为具有不同格式的字符串? :,例如(假设您正在处理波兰月份名称):

@[User::NewDataString] = 
LEFT((DT_WSTR,50)@[User::ToDate],2) + "-" +
(SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "maj" ? "may" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "stycz" ? "Jan" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "lut" ? "feb" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "mar" ? "mar" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "maj" ? "may" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "czerw" ? "jun" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "lip" ? "jul" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "sierp" ? "aug" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "wrzes" ? "sep" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "pazdzier" ? "oct" : 
SUBSTRING((DT_WSTR,50)@[User::ToDate], FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) + 1,  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",2) -  FINDSTRING((DT_WSTR,50)@[User::ToDate],"-",1) - 1)  == "listopad" ? "nov" : "dec" )
+ "-" + RIGHT((DT_WSTR,50)@[User::ToDate],4)

然后你应该将新变量作为参数传递.

(2)将数据格式更改为yyyy-MM-dd

您还可以使用表达式任务将月份转换为数值,例如:

@[User::NewDateString] = 
RIGHT((DT_WSTR,50)@[User::ToDate],4) + "-" +
RIGHT("0" + (DT_WSTR,50)DATEPART("mm", @[User::ToDate]),2) + "-" + 
LEFT((DT_WSTR,50)@[User::ToDate],2)

参考

> Polish Months Names
> Is it possible to perform a “LIKE” statement in a SSIS Expression?
> SSIS expression: convert date to string
> DATEPART SSIS Expression

点赞