使用当前月份名称将SSIS中的日期转换为字符串?
Convert Date To String in SSIS with current name of month?
我收到错误,
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
?
不知道具体错误,但我认为这可能是由于 SQL 服务器排序规则造成的,请尝试检查您的服务器排序规则。或者可能正在探索排序规则可能对您有所帮助。
您的包裹的 LocaleID 似乎不同于 1033(美国英语)。
这个值可以在包级别进行调整,因此所有底层任务都将继承它。
如果值是在 SQL 服务器端生成的
,也可能与 SQL 服务器登录的语言设置有关
检查哪些因素会影响 SSIS 中的日期区域设置的相关问题:
我同意@AlexanderVolok,看起来包裹的 LocalID 与英语不同,或者在操作系统区域设置中选择的日期时间文化信息不同。
(1) 使用条件更改月份名称? :
如果您可以更改这些 属性 那么它可能会解决问题,否则您在将日期转换为字符串的数据流任务之前添加一个 Expression Task使用条件运算符 ? :
的不同格式,例如 (假设您正在处理波兰月份名称):
@[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
我收到错误,
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
?
不知道具体错误,但我认为这可能是由于 SQL 服务器排序规则造成的,请尝试检查您的服务器排序规则。或者可能正在探索排序规则可能对您有所帮助。
您的包裹的 LocaleID 似乎不同于 1033(美国英语)。
这个值可以在包级别进行调整,因此所有底层任务都将继承它。
如果值是在 SQL 服务器端生成的
,也可能与 SQL 服务器登录的语言设置有关检查哪些因素会影响 SSIS 中的日期区域设置的相关问题:
我同意@AlexanderVolok,看起来包裹的 LocalID 与英语不同,或者在操作系统区域设置中选择的日期时间文化信息不同。
(1) 使用条件更改月份名称? :
如果您可以更改这些 属性 那么它可能会解决问题,否则您在将日期转换为字符串的数据流任务之前添加一个 Expression Task使用条件运算符 ? :
的不同格式,例如 (假设您正在处理波兰月份名称):
@[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