SSIS changes datetime variable to another format

I have an ADO.NET source in my SSIS package and I am trying to make the following request:

"SELECT * FROM Table WHERE DateAdded> '@ [User :: InputDateV]'"

On the other hand, I have a DateTime variable called InputDateV.

My problem is that in the value variable I can use the following date: 10/24/2012, but my Sql server this date is invalid and should be 10/24/2012.

How can I change the date and time format of my variable (mm / dd / yyyy)?

Thanks.

Sincerely.

Jose.

+4
source share
3 answers

The safest bet is to convert the DateTime variable to an ISO date format:

"'" + (DT_WSTR,4)YEAR(@[User::InputDateV]) + RIGHT("0" + (DT_WSTR,2)MONTH(@[User::InputDateV]),2) + RIGHT("0" + (DT_WSTR,2)DAY(@[User::InputDateV]),2) + "'" 

This is rated as "20121024" and as such is the perfect, single-valued date string for SQL Server.

+3
source

The datetime data type has no format. When you convert it to a string type, then yes, you can apply a format that matches the language, but inside datetime is an agnostic of the locale.

I preface my answer with an ADO.NET source that is much more painful than OLE counterparts.

In my playback, I have an Execute SQL Task that creates a table and stores some data in it. He then proceeds to the task of data flow.

enter image description here

In my data stream, I start by querying SELECT T.* FROM dbo.[Table] AS T There is currently no WHERE clause. This is here to allow data flow components to record the metadata of the original request.

enter image description here

To parameterize it, you need to return to the control flow level. Select the data flow task and in the "Properties" section, you will need to add an expression for the SqlCommand property of the ADO NET source.

Experience has shown that it is best to develop expressions for a variable and assign a variable to the Task property compared to creating it in the Task itself. If for some other reason this approach allows you to set a breakpoint and look at local residents and visually check the value. This cannot be done to express an object, especially if the specified expression causes a packet failure.

To this end, you will see that I have defined two variables in my SSIS package.

  • InputDateV - The data type is DateTime, and it has a value of 10/24/2012 12:01 AM . I added a time component just to display it, but you can disable it to suit your needs.
  • Query - The data type is String. These variables are EvaluateAsExpression = True, and the expression is "SELECT T.* FROM dbo.[Table] T WHERE T.DateAdded > '" + (DT_WSTR, 24)@[User::InputDateV] + "'" Here I force the value agnostic datetime value in the locale, but due to the magic of .NET code it will work.

Then I use @ [User :: Query] to configure [ADO NET Source]. [SqlCommand] and, oddly enough, everything works.

Otherwise, I misunderstood your definition regarding your variable names. If @ [User :: InputDateV] is actually a String type, then the above will not work for you. I created the variable @ [User :: InputDateS] of the data type string and assigned it the value 24/10/2012 . If I modify the @ [User :: Query] expression to use it, SQL Server will reject it because it cannot make this value as datetime data. Thus, the SSIS expression performs the translation. Change the expression to @ [User :: Query] so that it is "SELECT T.* FROM dbo.[Table] T WHERE T.DateAdded > '" + (DT_WSTR, 24)((DT_DATE) @[User::InputDateS]) + "'" and Bob is your uncle, green boxes through the package.

Link

+1
source

You can have an SSIS string variable and save the value in the format "mm / dd / yyyy". The value passed to prepare the request has single quotes ('), so this will work. If you want to support SSIS with a date date, prepare the request using the script component using System.DateTime as follows. then your request will be prepared. Hope this helps.

 public void Main() { // Map your variable using Dts.Variables["NameOfDateTimeVariable"].Value DateTime dt= DateTime.Now; var dateTime = string.Format("{0:mm/dd/yyyy}",dt); var query = string.Format("SELECT * FROM Table WHERE DateAdded > '{0}'",dateTime); //Map the query to your variable if you want Dts.TaskResult = (int)ScriptResults.Success; } 

Thanks Gowdhaman

0
source

Source: https://habr.com/ru/post/1441782/


All Articles