4

在 ssis 包中,我有String type variable V2内部表达式属性,我正在编写以下 sql 查询

"select * from mytable where date = " + @[System::StartTime]

但这给了我一个错误:

The data types "DT_WSTR" and "DT_DATE" are incompatible for binary operator "+". The operand types could not be implicitly cast into compatible types for the operation. To perform this operation, one or both operands need to be explicitly cast with a cast operator.

Attempt to set the result type of binary operation ""select * from table where date = " + @[System::StartTime]" failed with error code 0xC0047080.

即使我也尝试过,(DT_WSTR) @[System::StartTime] 但仍然没有任何建议?

4

1 回答 1

4

You need to change data type of both StartTime variable and [date] field from the query to string. Try this:

"select * from mytable where convert( varchar(10), [date], 120) = '" + SUBSTRING((DT_WSTR,50)@[System::StartTime],1, 10) + "'"

Which should return a proper query:

select * from mytable where convert( varchar(10), [date], 120) = '2013-05-22'

convert() will give you string like "2013-05-22". In my system (DT_WSTR) cast on @[System::StartTime] is returning string "2013-05-22 16:14:43", but if you have other settings, you'd have to construct the string from dateparts, if your default result would be for example "05/22/2013 16:14:43" due to other regional setting.

What is the verion of sql server you are using? and [date] field type exactly?

于 2013-05-22T14:23:07.957 回答