Date and Time Format in SSIS - sql-server

I have a date and time format:
Interval Start Time
1/13/16 1:30:00
1/15/16 10:30:00
Desired Result
Interval Start Time
13/01/2016 13:30:00 (24 Hr)
15/01/2016 10:30:00
The Interval Time is between 08:00 to 17:30.
I would like it to be: 13/01/2016 13:30 and 15/01/2016 10:30:00 and I devised this In SSIS derived Column:
(DT_DATE)(SUBSTRING([Interval Start Time],3,2) + "-" +
SUBSTRING([Interval Start Time],1,1) + "-" +
SUBSTRING([Interval Start Time],6,2) + " " +
SUBSTRING([Interval Start Time],9,1) == 1 ? "13" :
SUBSTRING([Interval Start Time],9,1) == 2 ? "14" :
SUBSTRING([Interval Start Time],9,1) == 3 ? "15" :
SUBSTRING([Interval Start Time],9,1) == 4 ? "16" :
SUBSTRING([Interval Start Time],9,1) == 5 ? "17" :
"[Interval Start Time]" )
+ ":" + SUBSTRING([Interval Start Time],11,2))
The error I get in SSIS is:
...The expression might contain an invalid token, an incomplete token, or an invalid element...
and I am not sure if the formula is correct in what I want it to do either. Any help would be much appreciated.

Before present a possible solution here, I want you be aware about some errors in your approach. Using expressions for this requeriment is very complex and hard to maintain, besides what happen when your Interval Start Time column have dates from October (10) to December (12) months, your string lenght will change and your hardcoded solution via SUBSTRING calls will return absurd data and produce error while package is running.
SOLUTION: Use Script component.
After creating your source, add a Script Component and link the source to it.
Configure the Script component to include Interval Start Time column by selecting it in Input Columns tab.
Add an output column, name it as you want and choose database timestamp (DT_DBTIMESTAMP).
Go to Script tab, and press the Edit Script... button.
Use the below code to overwrite the Input0_ProcessInputRow function.
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
/*
* Add your code here
*/
var str_timestamp = Row.IntervalStartTime.ToString();
string[] arr = str_timestamp.Trim().Split(' ');
string[] date = arr[0].Split('/');
string[] time = arr[1].Split(':');
string formatedDate = "20" + date[2] + "-" + date[0].PadLeft(2, '0') + "-" + date[1].PadLeft(2, '0');
int hour = Convert.ToInt32(time[0]);
int hour_24 = hour < 8 ? hour + 12 : hour;
formatedDate += " " + hour_24 + ":" + time[1] + ":" + time[2];
Row.MyColumn = Convert.ToDateTime(formatedDate);
}
In Row.MyColumn, replace MyColumn by the given name of your output column.
Save changes in Visual Studio and close the editor.
Now you can add a destination after the Script component and you will see the formatted IntervalStartTime as you need.
Let me know if this helps.

Related

How to Replace all occurrence of a character except last in a string in SSIS?

I have a string number like this:
1.000.000.00
So I have to delete all dots except last one, it will look like this:
1000000.00
Also if there is only one dot it should keep it.
Example:
INPUT | OUTPUT
-----------------------------------
1.00 | 1.00
1.000.00 | 1000.00
What I tried:
I create a derived column with this expression
LEFT([Column],FINDSTRING([Column],".",LEN([Column]) - LEN(REPLACE([Column],".",""))) - 1)
+ SUBSTRING([Column],FINDSTRING([Column],".",LEN([Column]) - LEN(REPLACE([Column],".",""))) + 1,LEN([Column]))
It keep the first dot not the last one.
You can try to use SSIS TOKEN() and TOKENCOUNT() functions.
TOKENCOUNT(column,".") == 4 ? TOKEN(1) + TOKEN(2) + TOKEN(3) + "." + TOKEN(4) :
( TOKENCOUNT(column,".") == 3 ? TOKEN(1) + TOKEN(2) + "." + TOKEN(3) :
( TOKENCOUNT(column,".") == 2 ? column :
( TOKENCOUNT(column,".") == 0 ? column: “" ) ) )

How to create a datepicker in react js

I tried different method to develop date picker in reactJS. I done with "react-datepicker" package and it return "Wed May 15 2019 12:54:33 GMT+0100" as a result but I need 12/12/2000 format
try to format it as you want
const pickerDate = new Date('Wed May 15 2019 12:54:33 GMT+0100')
const day = pickerDate.getDay() < 10 ? '0' + pickerDate.getDay() : pickerDate.getDay()
const month = pickerDate.getMonth() + 1 < 10 ? '0' + (pickerDate.getMonth() + 1) : pickerDate.getMonth() + 1
const year = pickerDate.getFullYear()
const formatedDate = `${day}/${month}/${year}`
you can also use third part libraries like moment

Concatenate a character to a TextBox value

How to concatenate a character after the field value in a text box in Telerik Reporting?
I tried the below way. Its not working. Is there any way to achieve this task?
= {Fields.OlderThan} + " Days"
= {Fields.OlderThan} + Days
= {Fields.OlderThan} Days
textBox24.Value expression [= {Fields.OlderThan} "Days"] is not valid:
If Fields.OlderThan = 10
Result should be "10 Days"
You should get rid of those {}. It should work just fine without them: = Fields.OlderThan + " Days".
I also like to handle the case where the field might be null, because if Fields.OlderThan is null nothing will get printed in your text box:
= IsNull(Fields.OlderThan,"") + " Days"

Date does not display properly while export from IE

Please find below code:
var sReportDate = new Date();
var sReportFormattedDate = sReportDate.toDateString() + " " + sReportDate.toLocaleTimeString() + " " + sReportDate.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1];
sTitle = sTitle + "\nReport Extracted on:" + sReportFormattedDate +"\n\n";
return sTitle;
The code formats the date. And the returned value is used in exported csv of angular-ui-grid.
When I export the grid from chrome and opens csv file in MS Excel, the date displays properly. But when I export the same grid from IE 11 and opens the csv file in MS Excel, the date displays with so many special characters.
The below statement displays the date in correct format on IE 11 console:
var sReportFormattedDate = sReportDate.toDateString() + " " + sReportDate.toLocaleTimeString() + " " + sReportDate.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1];
Tue Sep 22 2015 ‎3‎:‎17‎:‎35‎ ‎PM GMT+0800 (Malay Peninsula Standard Time)
But while export from IE 11, the time portion (string between 2015 and PM) displays many special characters in MS Excel.
Why toLocaleTimeString is exported properly from Chrome and why is it not exported properly from IE11 ?
How do I display the date in proper format while export from IE 11 ?

Help with SSIS Package

Hi all i've got a complex SSIS package, however i'm hitting my head against a brick wall with a particular part. I've got a maintenance part that will delete files that are older than 3 months old (from today's date). The files all have the date in the filename, for example AA-CDR-20110606030000-2-001A648E6F74-026874.xml
So i've written a task that will loop over all the files in a particular folder using a Foreach loop, then i have a script that will load in the filename using a variable set using the foreach loop. Then delete the file using the script below. This works fine when running in debug mode. However when trying to execute this on the server i get a failure with a "System.FormatException: String was not recognized as a valid DateTime.". I really dont understand why, any ideas?
DateTime deleteDate = DateTime.Today.AddMonths(-3);
String file = Dts.Variables["FileName"].Value.ToString();
String fileDateString = file.Substring(42, 8);
String fileDateYear = fileDateString.Substring(0, 4);
String fileDateMonth = fileDateString.Substring(4, 2);
String fileDateDay = fileDateString.Substring(6, 2);
DateTime fileDateTime = Convert.ToDateTime(fileDateDay + "-" + fileDateMonth + "-" + fileDateYear);
if (fileDateTime < deleteDate)
{
System.IO.File.Delete(file);
}
It seems that there is a file on the production server that does not follow the pattern and the date cannot be extracted from its name.
Try to use something like this:
try
{
DateTime fileDateTime = Convert.ToDateTime(fileDateDay + "-" + fileDateMonth + "-" + fileDateYear);
if (fileDateTime < deleteDate)
{
System.IO.File.Delete(file);
}
}
catch (System.FormatException)
{
// log the Dts.Variables["FileName"] value here to see why it could not be parsed
}

Resources