SSIS derived column convert string to date - ssis

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

Related

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

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.

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))

Convert string to Date in ssis

I have tried converting a date in the format 23/12/2001 to 2001-12-23 using the following in ssis but I keep getting an error.
ISNULL(Date_Of_Birth) ? NULL(DT_DBTIMESTAMP) : (DT_DBTIMESTAMP)
(SUBSTRING(Date_Of_Birth,FINDSTRING(Date_Of_Birth,"/",2) + 1,4) + "-" + RIGHT("0" +
SUBSTRING(Date_Of_Birth,1,FINDSTRING(Date_Of_Birth,"/",1) - 1),2) + "-" + RIGHT("0" +
SUBSTRING(Date_Of_Birth,FINDSTRING(Date_Of_Birth,"/",1) + 1,FINDSTRING(Date_Of_Birth,"/",2) -
FINDSTRING(Date_Of_Birth,"/",1)),2))
I used the one on todds blog (http://toddmcdermid.blogspot.co.uk/2008/11/converting-strings-to-dates-in-derived.html) but it seems not to work for me for some reason. Can anyone explain how I can go about this
Maybe write the .NET code in the script task to convert the date format is the better idea:
Dim dateStr As String = "23/12/2001"
Dim result As DateTime
If DateTime.TryParseExact(dateStr, "dd/MM/yyyy", Nothing, Globalization.DateTimeStyles.None, result) Then
MsgBox(result.ToString("yyyy-MM-dd"))
End If