SSIS using derived column to convert data from mm/d/yyyy to dd/mm/yyyy - ssis

I have a SSIS package that inserts data from csv into SQL database.
This file has a data column that the format is different from SQL. I am getting error because the SSIS says that the data can not be convert.
The datacolumn is like this 11/9/2022 12:00:00 AM this data is related to 9th of november.
I need to use derived column to convert this data into dd/mm/yyyy.
I was trying using SUBTRING but I noticed that as firts character are the month we do not have the same number of character before the first / and the same for day.
I was wondering if I could try something like that
SUBSTRING([Dt],FINDSTRING("/",[Dt],1) + 1,FINDSTRING("/",[Dt],2) - 1) + "/" + LEFT([Dt],FINDSTRING("/",[Dt],1) - 1) + "/" + SUBSTRING([Dt],FINDSTRING("/",[Dt],2) + 1,4)
But I am getting the same error
How can I do to convert 11/9/2022 into this 09/11/2022 (dd/mm/yyyy) using derived column?

You were on the right path, but had the wrong syntax for FINDSTRING - the string to search comes first.
After a bit of trial and error I came up with this:
((FINDSTRING([Dt],"/",2) - FINDSTRING([Dt],"/",1)-1)==1?"0":"") + SUBSTRING ([Dt],FINDSTRING([Dt],"/",1)+1, FINDSTRING([Dt],"/",2) - FINDSTRING([Dt],"/",1)-1) + "/" + (FINDSTRING([Dt],"/",1)==2?"0":"") + SUBSTRING ([Dt],1,FINDSTRING([Dt],"/",1)-1) + "/" + SUBSTRING ([Dt],FINDSTRING([Dt],"/",2)+1,4)
SSIS functions can't be multi-line so the above is on 1 line, this is over multiples lines for ease of reading
((FINDSTRING([Dt],"/",2) - FINDSTRING([Dt],"/",1)-1)==1?"0":"") +
SUBSTRING ([Dt],FINDSTRING([Dt],"/",1)+1, FINDSTRING([Dt],"/",2) - FINDSTRING([Dt],"/",1)-1) + "/" +
(FINDSTRING([Dt],"/",1)==2?"0":"") +
SUBSTRING ([Dt],1,FINDSTRING([Dt],"/",1)-1) + "/" +
SUBSTRING ([Dt],FINDSTRING([Dt],"/",2)+1,4)
The 1st and 3rd lines are to handle if the day or month is single number, and adds a 0 of it is.

This stuff is always easier in c#. Assuming you have a string, then use script componentas follows:
var newCol = DateTime.ParseExact(row.ColumnName
, "MM/dd/yyyy hh:mm:ss tt"
, System.Globalization.CultureInfo.InvariantCulture)
.ToString("dd/MM/yyyy");
All that being said. It is not great practice to carry strings around that represent dates. Once properly loaded into a date, keep it as a date until the final presentation layer.

Related

extract date from file name using SSIS expression

HT,
Using Microsoft SSIS.
I have a input CSV file called - LatLong_WD_Locations_06-21-2021.
I want to extract date from this file name in 20210621 format using SSIS expression and store in to a variable called v_FileDate which is Int32 type.variable v_FileName is a string type.
I tried with
(DT_I4) REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "_", 2)+1,10),"-","")
but its not working.
Can I have some help here, please.
TIA
Almost there. You specified 2 for the occurrence argument in FINDSTRING expression. So it is finding the _ before Location in the file name giving you a result of Locations_. Since that is not a integer it is throwing an error.
Change the 2 to a 3:
(DT_I4) REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "_", 3)+1,10),"-","")
The above would account for if the V_FileName has a file extension. It would not get you the final format of yyyyMMdd. See below...
You could also simplify and use RIGHT expression. Get the right 10 characters of the string and then replace:
(DT_I4) REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-","")
I updated the above statement to account for if v_FileName had an extension. That still does not give your final format of yyyyMMdd. See below...
Those 2 expressions above will get the date out of the v_FileName, but in format MMddyyyy. Now you will have to parse out each part of the date and put it back together using one of the above statements. The example below is using the one with RIGHT:
(DT_I4) SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 5,4)
+ SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 1,2)
+ SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 3,2)
If you ever have Date on the last 10 positions in file name solution is very simple. But if that is not case, write me below in a comment and I will write a new expression.
Solution explained step by step:
Get/create variable v_FileDate with value LatLong_WD_Locations_06-21-2021
For check create DateString with expression
RIGHT( #[User::v_FileDate], 4) +
LEFT (RIGHT( #[User::v_FileDate], 10), 2) +
LEFT (RIGHT( #[User::v_FileDate], 7), 2)
Create a final variable DateInt with expression
(DT_I4) #[User::DateS]
How variable should like:
Or you can with a single variable (It would be better with a single variable).
(DT_I4) (
RIGHT( #[User::v_FileDate], 4) +
LEFT (RIGHT( #[User::v_FileDate], 10), 2) +
LEFT (RIGHT( #[User::v_FileDate], 7), 2)
)
Final Result
Using script task...
Pass in v_FileDate as readwrite
Pass in v_FileName as readonly
Code:
//Get filename from SSIS
string fileName = Dts.Variables["v_FileName"].Value;
//Split based on "_"
string[] pieces = fileName.Split('_');
//Get the last piece [ref is 0 based and length is actual]
string lastPiece = pieces[piece.Length -1];
//Convert to date
DateTime d = DateTime.ParseExact(lastPiece, "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture);
//Convert to int from string converted date in your format
Dts.Variables["v_FileDate"].Value = int.Parse(d.ToString("yyyyMMdd"));
Some work around -Working for me in (YYYYMMDD) format.
Thanks #keithL
(DT_I4)( REPLACE(RIGHT(#[User::v_FileName], 5),"-","")+REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "", 3)+1,2),"-","")+REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "", 3)+3,4),"-",""))

How do I get a date in the format of "mmm-YYYY" given a month and a year in SSRS

I have the following expression in my SSRS template:
=UCase(Left(MonthName(Fields!theMonth.Value), 3)) + "-" + Fields!theYear.Value
If I take out this bit:
+ Fields!theYear.Value
The expression works as expected and returns "FEB-" if theMonth is 2.
However SSRS returns an error when I add the year back in.
Any pointers would be appreciated.
Use & NOT + to concatenate strings.
+ works much of the time except when mixing types. Since there's an integer, the expression assumes that you are doing math(s).
& is used for strings and will convert numbers to strings for the expression.
The expression
=UCase(MonthName(5, 1)) + "-" + 2018
throws an error.
The Value expression for the textrun
‘Textbox1.Paragraphs[0].TextRuns[0]’ contains an error: Input string
was not in a correct format.
But changing just the + to & does not get an error.
=UCase(MonthName(5, 1)) & "-" & 2018
On another note:
MonthName has an optional parameter for abbreviations - just add ,1 after theMonth.Value - UCase(MonthName(Fields!theMonth.Value, 1), 3)
The following seems to work:
=UCase(Left(MonthName(Fields!theMonth.Value), 3)) + "-" + UCase(Fields!theYear.Value)

Convert DDMMYYYY varchar value to MM-dd-yyyy in SSRS

I wanted to convert DDMMYYYY formated data (data type is varchar) to MM-dd-yyyy format using SSRS expression only.
I tried the following but it is showing #Error
=IIF(NOT(IsNothing(Fields!DoB.Value)),CDate(Fields!DoB.Value).ToString("MM-dd-yyyy"),Nothing)
=IIF(NOT(IsNothing(Fields!DoB.Value)),Format(CDate(Fields!DoB.Value),"MM-dd-yyyy"),Nothing)
It I try like below then it shows MM-dd-yyyy,
=Format(Fields!DoB.Value, "MM-dd-yyyy")
I don't understand why you pass it as a string in your report. Anyway here it is.
I tested it using this expression:
= Mid("25052016",3,2) + "-" + Left("25052016",2) + "-" + Right("25052016", 4)
and this is my output:
05-25-2016
Basically what I did here is just separate your string in three chunks separated with - to achieve your goal.
So in your example expression you could try it like this:
=IIF(NOT(IsNothing(Fields!DoB.Value)),(Mid(Fields!DoB.Value,3,2) + "-" + Left(Fields!DoB.Value,2) + "-" + Right(Fields!DoB.Value, 4)),Nothing)
Note: I'm not 100% sure that the expression just above this comment will work in your end because I haven't tested it but atleast I showed to you my concept on my other example.
Have you tried to parse the field value, like this?
=Format(CDate(Mid(Fields!DoB.Value,3,2) & "-" & Left(Fields!DoB.Value,2) & "-" & Right(Fields!DoB.Value,4)), "MM-dd-yyyy")

SSIS expression for date comparison?

My expression is like following:
DATEDIFF("dd",(DT_DATE)(SUBSTRING(#[User::strExcelFileName],15,2) + "-" + SUBSTRING(#[User::strExcelFileName],18,2) + "-" + SUBSTRING(#[User::strExcelFileName],21,4)),GETDATE()) > #[User::intDaysCount]
My file : TempConfigPr_06172013.xlsx
The problem is it's saying error while converting from "DT_WSTR" to data type "DT_DATE"...Could any one please help, where I'm going wrong and how to solve this?
Note: I checked,this expression is working fine.
DATEDIFF("dd",(DT_DATE)("11-18-2010"),GETDATE()) > #[User::intDaysCount]
Editing expressions in SSIS can be painful. I find when they aren't working as expected, it's best to break them into multiple variables and have them feed off each other.
By doing so, I quickly determined that your root problem was this expression was not generating a valid date value. It generated 61-20-3.xl
SUBSTRING(#[User::strExcelFileName],15,2) + "-"
+ SUBSTRING(#[User::strExcelFileName],18,2) + "-"
+ SUBSTRING(#[User::strExcelFileName],21,4)
I created a variable strExcelFileDate of type string and I used the expression to create a string that is the 8 characters following the underscore.
SUBSTRING(#[User::strExcelFileName], FINDSTRING(#[User::strExcelFileName], "_", 1)+1, 8)
That string value, 06172013, cannot be directly cast to a date, which is a pity. I needed to slice and dice that string into something that can be cast to a date data type. A new Variable named dtExcelFileDate with a type of DateTime was created. I used an expression on that to transform the string into yyyy-mm-dd and then cast that entire thing to a DT_DATE data type.
(DT_DATE) (RIGHT(#[User::strExcelFileDate], 4) + "-"
+ SUBSTRING(#[User::strExcelFileDate], 1,2)
+ "-" + RIGHT(SUBSTRING(#[User::strExcelFileDate],1,4), 2))

SSIS derived column convert string to date

I am trying to convert a string into Date using Derived column and I am using Flat File Source.
I read bunch of articles on stackoverflow and followed the same steps but I am unable to convert my string into date format.
Here is the string
Tue Nov 25 17:32:03 EST 2008
And I want to show it like
2008-nov-25
I tried using the following code
(DT_DBDATE)(SUBSTRING(dateCreated,24,4) + "-" + SUBSTRING(dateCreated,4,3) + "-" + SUBSTRING(dateCreated,8,2))
I also tried to use
(DT_DATE)(SUBSTRING(dateCreated,24,4) + "-" + SUBSTRING(dateCreated,4,3) + "-" + SUBSTRING(dateCreated,8,2))
(DT_DBTIMESTAMP)(SUBSTRING(dateCreated,24,4) + "-" + SUBSTRING(dateCreated,4,3) + "-" + SUBSTRING(dateCreated,8,2))
Is this the correct way to convert the string into date format?
I dont think its possible to display the in the format of yyyy-mon-dd as per BOL. I checked your expression and after minor changes, was able to make it to work. You cannot typecast your string into a string again (You want month name in your output). Even if you were to use the DATEPART function, it would again amount to concatenating different parts into a string.
The expression that works with your input is as follows (replace #dateCreated with whatever you want to).
SUBSTRING(#[User::dateCreated],24,5) + "-" + SUBSTRING(#[User::dateCreated],4,4) + "-" + SUBSTRING(#[User::dateCreated],8,3)
Thanks Rajiv!
So The final expression which gives the desired result is:-
(DT_DBTIMESTAMP)(SUBSTRING(dateCreated,24,5) + "-" + SUBSTRING(dateCreated,4,4) + "-" + SUBSTRING(dateCreated,8,3) + " " + SUBSTRING(dateCreated,11,9))
The input being :-
Tue Nov 25 17:47:41 EST 2008
And the output is:-
2008-11-25 17:47:41.000