I am trying to use Ado.Net Sql command to select data from a table with a date filter.
SELECT COLUMN1
, COLUMN2
FROM TABLENAME
WHERE DATE_INSERTED > @[User::LastInsertDate]
Both Date_Inserted
and @[User::LastInsertedDate]
are of type DateTime
. When I try to evaluate expression in the expression builder l get the following error;
The expression might contain an invalid token, an incomplete token, or an invalid elemnt, it might not be well-formed, or might be missing part of a required element such as a parenthesis.
Problem diagnosis from original revision of accepted answer:
Here is my understanding of your question. I believe that you created two variables under package scope. A variable named
LastInsertDate
of DateTime data type and another variable namedSqlQuery
of String data type to store the SQL SELECT command.You set the EvaluateAsExpression property on variable
SqlQuery
toTrue
. You then entered the following commandSELECT COLUMN1, COLUMN2 FROM TABLENAME WHERE DATE_INSERTED > @[User::LastInsertDate]
When you clicked EvaluateAsExpression, you got the following error message:
Expression cannot be evaluated. Additional information: Attempt to parse the expression "SELECT COLUMN1, COLUMN2 FROM TABLENAME WHERE DATE_INSERTED > @[User:LastInsertDate]" failed. The expression might contain an invalid token, an incomplete token, or an invalid element. It might not be well-formed, or might be missing part of a required element such as a parenthesis.
The issue here is that the value you are trying to store in the variable SqlQuery is not enclosed within double quotes. The string value of the dynamic query should be enclosed within double quotes.
While enclosing the text in double quotes, you cannot use the datetime variable
LastInsertDate
as it is. You need to convert the date time variable to string but if you simply convert datetime value to string, you might land into unexpected format. To be on the safe side, I would recommend usingDATEPART
function to translate the datetime value to string of formatYYYY-MM-DD hh:mi:ss
. Here is the complete expression that will do that.When you click Evaluate Expression, you will see the string with the date time value in the Evaluated value section.
Hope that helps.