How to generate dynamic file naming for flat file in ssis? - ssis

Hi I am trying to use the expression column in flat file connection manager to generate file with dynamic naming e.g
File-ddmmyyyyhhmmss.txt
I understand that I can use getdatepart but what is the exact method call to get the hh, mm and ss? Can anyone advise?

Pragmatic works SSIS Expression Cheat Sheet is a good start. You can expand to your situation by using (DT_WSTR, 2) datepart("HH",getdate()) to get the hour portion, (DT_WSTR, 2) datepart("n",getdate()) to get the minutes and (DT_WSTR, 2) datepart("s",getdate()) to get the seconds. You will need to add logic in to ensure that values less than 10 still get two digits (i.e. 01 05) etc. For Day, month and Year, there are function DAY() MONTH() and YEAR() that shortcut the equivalent datepart() functions.

Related

How to use expression builder to create a file name with date even for the first day of the month

I have an SSIS package that runs each morning to pull the previous days file from an FTP server. I am using the code below to create the file name using the previous date. Everything works great with this except when today's date is the first day of the month. for example, if ran today (3/1/2021) this returns name_of_file_20210328.xml.gz, however yesterday's date is 2/28/2021 not 3. How do i update this to say if today's date is beginning of month return mm - 1?
"name_of_file_" + (DT_STR,4,1252)(DATEPART("yyyy",GETDATE())) + (LEN((DT_STR,2,1252)(DATEPART("MM",GETDATE()))) == 2 ? (DT_STR,2,1252)(DATEPART("MM",GETDATE())) : "0" + (DT_STR,2,1252)(DATEPART("MM",GETDATE()))) + (LEN((DT_STR,2,1252)(DATEPART("dd",DATEADD( "day",-1, GETDATE())))) == 2 ? (DT_STR,2,1252)(DATEPART("dd",DATEADD( "day",-1, GETDATE()))) : "0" + (DT_STR,2,1252)(DATEPART("dd",DATEADD( "day",-1, GETDATE())))) +
".xml.gz"
Create a variable, Yesterday of type DateTime. Specify that it uses an expression and use the following expression. This provides a consistent reference point you can test against and if you disable the expression, allows you to specify a date for boundary/special case checking (like a leap year 2020-03-01)
DATEADD("DAY", -1, #[System::StartTime])
The next steps, especially if you're starting out, is to build the date parts in separate variables. It doesn't cost any extra to use lots of variables in your package and makes troubleshooting so much easier.
Add a new variable, YearString of type String.
(DT_WSTR, 4)datepart("YYYY", #[User::Yesterday])
That was easy.
Now we need to deal with create a zero, left padded string. Right now, your expression looks like it's trying to determine if day or month has 2 digits. I have a cleaner expression.
We're going to convert the day/month to a string and then prepend a zero to it. For Jan-Sep, we'll end up with a 2 character expression, Oct-Dec, we'll have a three character expression e.g. 011. For Day, similar bit where it's 01-09 or 010-031. We will then take the last two characters from the string. For the two character strings, it's a no-operation and for the three character variant, it gets us what we want.
Add Variable MonthString, as type string, to your package
RIGHT("0" + (DT_WSTR, 2)datepart("MONTH", #[User::Yesterday]), 2)
Add Variable DayString, as type string, to your package
RIGHT("0" + (DT_WSTR, 2)datepart("DAY", #[User::Yesterday]), 2)
Take a moment and look at your Variable collection. You can see that you have all the building blocks needed to properly construct your YYYYMMDD string. If something is wrong, it's small enough snippet to play with it. If not, break it up into smaller Variables.
Now that we have defined #Yesterday and then built YearString, MonthString and DayString off of Yesterday, all we have to do is bring it all together with the concatenation + operator
Back to the Variable well, creating #CurrentFileName of type string
"name_of_file_" + #[User::YearString] + #[User::MonthString] + #[User::DayString] + ".xml.gz"
Results in a value of name_of_file_20210216.xml.gz
Not addressed in this answer but things you should think about
What happens when the job doesn't run at all today (server fails horrifically, the data source is down, etc)? To pick up and process 2 days ago file, you would need to edit this package, run it through whatever change review process is applicable, deploy to prod, run it and then go back to the process yesterday file package.
That's not fun to type much less consider. You certainly aren't going to change the server time to trick the expression into being yesterday.
I am an advocate for passing the run date to the package (mechanism depends on how you deploy/run packages). In that situation, it's been my experience that it's a far easier bureaucracy fight to change the calling parameter (because no "code" has changed) than to get an emergency code change run through.

Date type convert in SSIS

Data set contains some 2020-02-01T19: 43: 03 data. How can I convert this to 2020-02-01 19:43:03 in SSIS. I tried (DT_DATE), (DT_DBDATE), (DT_DBTIMESTAMP) but it didn't.
Thanks
This should be fairly simple depending on the approach you wish to take :
You can create a script transformation task in which you can apply C# String.Split function to remove the T.
You can use SQL and use a SUBSTRING function which allow you to take the first part of the data till T and the next part of the datetime from T onwards.

How can I format the date from mysql (I'm using node.js)?

I'm using node.js to select info from my mysql database and I have a datetime column, but I'm not getting the format I want.
This is my sql code
SELECT id, data, titulo, subtitulo, texto, DATE(datahora_cadastro) as data FROM sig_noticias
I need the data to be like this: DD/MM/YYYY
But I'm getting this: Thu Mar 30 2017 00:00:00 GMT-0300 (GMT-03:00)
Easiest way to format dates in Javascript is by far using a modern date / time library. I personally use MomentJS and it works well. I highly recommend this solution if you plan on working with dates in even just a few areas of your app.
Using moment, you could simply call moment(myDate).format("DD/MM/YYYY");.
If you really want to stick to pure Javascript, however, here's how to get the format you want:
const formattedDate = `${myDate.getDate() + 1}/${myDate.getMonth() + 1}/${myDate.getFullYear()}`;
Javascript's built-in date functions are pretty confusing. Here, for instance, the getDate and getMonth methods return the index of the date / month, and not the actual date / month, so we need to add +1 to those.
Using vanilla Javascript then becomes even harder when dealing with date manipulation (adding, subtracting) and timezones.

Date conversion in SSIS

anyone has an idea how to convert date to the format for example Monday,15-January-1990 in SSIS.
This is what I tried converting birthdate to the example format above:
(DT_STR,10,1252)DAY(BirthDate) + ","
+ (DT_STR,2,1252)DATEPART("dd",BirthDate)
+ "-" + (DT_STR,15,1252)MONTH(BirthDate)
+ "-" + (DT_STR,4,1252)DATEPART("yy",BirthDate)
The example output i get is this : 1,1-9-1965.
What you've done so far is as far as you can get with built-in SSIS Date/Time functions.
AFAIK, SSIS does not have a built-in WEEKFDAYNAME or MONTHNAME functions. You can see that even in the Expression Builder, under Date/Time Functions, there are only the following. MONTH returns interger of month and DAY returns the actual date, not the weekday. To get the weekday, you have to pass in "dw" as a parameter to the DATEPART function (see example below). Today is Thu = weekday #5
That said, there are a couple of "round about ways" to achieve what you want.
1. Use a scripting Task or Scripting Component to take in a date parameter and output the translated / transformed date string.
2. Use a couple of variables and an expression task as detailed here => SSIS - How To Use Expression Task To Get Day Name and Month Name In SSIS Package

Adding Time Stamp Values in SSRS reports

I am trying to create a SSRS Report and am facing a problem in some calculation.
I have few columns that contains the values like 101.45, 25, 0.15, 3.30.
Actually these values are time values, now adding all these leads to the result 129.9.
Now the problem is when the decimal part exceeds .60 I need to add one to number, like the final result I need is 130.30.
Can any one please help me solving this problem.
Thanking you in advance.
You cannot add the values directly. You need to first convert the values into scale of 100. Add it and then convert it back to your scale of 60.
SELECT SUM(Floor(Column1)+(Column1%1)*100/60) FROM TimeAdd
http://sqlfiddle.com/#!3/b5d55/8
For doing this in SSRS
=
(Int(ReportItems!TextBox1.Value) + (ReportItems!TextBox1.Value Mod 1)*100/60)
+
(Int(ReportItems!TextBox2.Value) + (ReportItems!TextBox2.Value Mod 1)*100/60)
+
(Int(ReportItems!TextBox3.Value) + (ReportItems!TextBox3.Value Mod 1)*100/60)
+
(Int(ReportItems!TextBox4.Value) + (ReportItems!TextBox4.Value Mod 1)*100/60)
But the above will give you 130.5 but you would probably want 130.3. You need to convert the above sum back into scale of 60. either you can store everything in another variable and then apply the scaling or write a big piece of code.
=INT(ReportItems!AbsoluteSum.Value) + (ReportItems!AbsoluteSum.Value Mod 1)*60/100
Just a suggestion It is easier to solve something in SQL then in report design.
You can create custom fields in your dataset where you can write expression which convert your time value to some integer value, or decimal value.
This process you need repeat for all fields from database that participate in your calculation, where finally in your dataset you have let's say, seven standard columns from database, and seven custom columns, which you can later working with.
Other solution could be writing some custom code in SSRS, where you can create function, which accepts parameters, and do calculations for you.