SSIS Wildcard Flat file connection - sql

I want to load data from some csv files in a certain directory into a sql server table. The files are in the following format File_DDMMYYYHHMMSS.csv however I only want to select the files from the current day, the time doesn't matter. Is there a way to search files in this way?
I have tried using a foreach loop and looking for only *.csv and setting a variable and then using:
LEFT( #[User::FileName],5 ) +
RIGHT("0" + (DT_WSTR,2)DatePart("dd", GetDate()), 2) +
RIGHT("0" + (DT_WSTR,2)DatePart("mm", GetDate()), 2) +
RIGHT((DT_WSTR, 4) DATEPART("yyyy", GetDate()), 4)
What i need is something like :
#filename + Todays date + "__"

Doing a quick google search yielded this link that explains the exact method of doing this. Its a dirty version of doing stuff, but gets the job done effectively.

Related

Challenge to change source date formats to desired date formats

Someone, please assist me in solving this challenge:
I am using Zappsys Json Source on my dataflow and trying to transfer date to the OLEDB destination.
Googlespreadsheet Source: ActualClosingDate : 7/29/16 as m/dd/yy format
I want to convert it to OLEDB destination as
2016-07-29 15:00:00:000 format using derived column expression.
Thank you
Take a drived column component and convert the date as expected like following example:
(DT_STR,4,1252)DATEPART("yyyy",GETDATE()) + RIGHT("0" + "-" + (DT_STR,2,1252)DATEPART("mm",GETDATE()),2) + "-" + RIGHT("0" + (DT_STR,2,1252)DATEPART("dd",GETDATE()),2) + "-" + " " + (DT_STR,2,1252)DATEPART("hour", GETDATE()) + ":" + (DT_STR,2,1252)DATEPART("minute", GETDATE())+ ":" + (DT_STR,2,1252)DATEPART("second", GETDATE()) + ":" + (DT_STR,2,1252)DATEPART("millisecond", GETDATE())
To convert this value to a DateTime value, you can simply use a cast operator in a derived column as follows:
(DT_DBTIMESTAMP)[ActualClosingDate]
If you are looking to store this column as a string with a specific format (since DateTime columns have no format) just reconvert it to a string, and it will automatically ISO formatted:
(DT_WSTR,50)(DT_DBTIMESTAMP)[ActualClosingDate]

Data not getting fetched from Source using ODBC Connection and passing the SQL Query from a variable

I have a source query, wherein I am fetching data from Horton using ODBC connection
Select * from Table1 Where CreationDate > '2020-09-24 00:00:001'
When I run this query manually it runs fine, but when I run my SSIS Package no data is being fetched.
Please note I am passing this SQL Query from a variable and all variables are correctly passed. I have checked it using Edit Breakpoints.
This are the steps I followed to pass the query from a variable
The issue with your query appears to be the default formatting for translating an SSIS DateTime type to a string representation. The ODBC source appears to need a YYYY-MM-DD hh:mm:ss:mss (where mss is milliseconds but not the correct format code).
I tend to break my long expression into multiple variables and then have a "simple" final form that puts them all together.
Date_YYYYMMDD -> (DT_WSTR, 4) YEAR(#[System::ContainerStartTime]) + "-" + RIGHT("0" + (DT_WSTR, 2) MONTH(#[System::ContainerStartTime]),2) + "-" + RIGHT("0" + (DT_WSTR, 2) DAY(#[System::ContainerStartTime]),2)
That builds my YYYY-MM-DD format string. Data type is String
Date_HHMMSSms -> RIGHT("0" + (DT_WSTR, 2) DATEPART( "Hour", #[System::ContainerStartTime] ),2) + ":" + RIGHT("0" + (DT_WSTR, 2) DATEPART( "Minute", #[System::ContainerStartTime] ),2) + ":" + RIGHT("0" + (DT_WSTR, 2) DATEPART( "Second", #[System::ContainerStartTime] ),2) + ":" + RIGHT("0" + (DT_WSTR, 3) DATEPART( "Millisecond", getdate() ),3)
This builds out the time component. I always advocate for using the System variable ContainerStartTime instead of GETDATE() as getdate is evaluated every time we access it while container start time is constant for the execution of a package. What I discovered and can't wait to blog about is the the expression language doesn't appear to get the milliseconds out of a ContainerStartTime but does work correctly for GETDATE() calls.
The final date variable then becomes something like
Date_Filter -> #[User::Date_YYYYMMDD] + " " + #[User::Date_HHMMSSms]
Which then makes my query usage look like
Query_Source -> "Select * from Table1 Where CreationDate > '" + #[User::Date_Filter] + "'"

SQL Query Ran By Date Minus 1 Year

I am trying to run a query to display results between 2 dates, and then display the results from those same dates but a year prior. This is what I have-
where (a.Invdate between '11/1/2015' and '11/30/2015')
will eventually use parameters so it would look like
where (a.Invdate between #StartDate and #EndDate)
But I then want to be able to run the same query with the same date range but 11/1/2014 and 11/30/2014 instead.
How can I go about achieving this?
DATEADD is the best way to do this. also you can use the log way, to parse each datepart so:
convert(VARCHAR(2),datepart(mm,'11/30/2015')) + '/' + convert(VARCHAR(2),datepart(dd,'11/30/2015')) + '/' + convert(VARCHAR(4),convert(INTEGER,datepart(yy,'11/30/2015')) - 1) AS newDate
(I vot you + because you are new user) and please be careful next time then post a question

SQL Server Reporting Services Subscription with dynamic parameters

I have a custom report in SQL Server Reporting Services.
This report has a few parameters, like: Requested date start, Requested data to, donor.
I would like to setup a scheduled email.
However I would like to assign the "Requested date start" value to = (current datetime-1 month)
and assign the "Requested date end" value to = current datetime
Any tips on how to do this?
This is quite simple, see my answer here:
SQL Server: calculating date ranges
What you have to end up doing is creating these calculations as a dataset and then use the "Use Default" checkbox in the subscription itself:
Write a stored procedure and get the following from the sql in the stored proc:
select #FileName as FILENAME, #PATH as PATH, 'EXCEL' as RENDER_FORMAT , 'Overwrite' as WRITEMODE,
'True' as FILEEXTN , 'null' as USERNAME, 'null' as PASSWORD
Call the proc from the subscription
Anil Madan
I had a similar issue. I created the subscription with an initial set of parameters and set the run time. Then I created a sproc and a job to run the sproc before the schedule run time of the subscription. Your date math will be different from mine depending on your needs.
I then copied the parameter settings to notepad, found the parts I needed to change and put them on their own lines. Then put them in the sproc as below.
UPDATE ReportServer.dbo.Subscriptions
SET Parameters =
(SELECT '<ParameterValues><ParameterValue><Name>Summary</Name><Value>PODETAIL</Value></ParameterValue><ParameterValue><Name>requisitioner_erp_user_id</Name><Value>51</Value></ParameterValue><ParameterValue><Name>requisitioner_erp_user_id</Name><Value>125</Value></ParameterValue><ParameterValue><Name>date_range_type</Name><Value>3</Value></ParameterValue><ParameterValue><Name>po_date_end</Name><Value>'
+
(SELECT FORMAT(DATEADD(wk, DATEDIFF(wk, 1, GETDATE()), -1), 'MM/dd/yyyy') AS po_date_end)
+
'</Value></ParameterValue><ParameterValue><Name>po_date_start</Name><Value>'
+
(SELECT FORMAT(DATEADD(wk, DATEDIFF(wk, 0, GETDATE()) - 1, 0), 'MM/dd/yyyy') AS po_date_start)
+
'</Value></ParameterValue></ParameterValues>'
)
WHERE ReportServer.dbo.Subscriptions.SubscriptionId IN ('B6645FD3-DE27-4551-8331-C0135305CC79')

SSIS - cast a date to use in an expression

I want to cast a date into ISO format, and use it as a string in an expression in SSIS.
This is what I type in T-SQL
select convert(varchar(8), GetDate(), 112)
and this is what I get back
20100630
My goal - is to create an archive folder based on the date. I have the File System Task part of creating a folder sorted, I can do that ... I just need to be able to Cast the date to a string, so I can use that.
Thanks in advance.
Old question, better-er answer, from Adrian's comment here:
Try an intermediate cast to type DT_DBDATE. Casting to WSTR gives a
string in format "YYYY-MM-DD", which is easily tidied up:
REPLACE((DT_WSTR,200)(DT_DBDATE)GETUTCDATE(),"-","")
Or use without the REPLACE to get the YY-MM-DD
You'll need to add an expression:
RIGHT((DT_WSTR, 4) DATEPART("yyyy", GetDate()), 4) +
RIGHT("0" + (DT_WSTR,2)DatePart("mm", GetDate()), 2) +
RIGHT("0" + (DT_WSTR,2)DatePart("dd", GetDate()), 2)
This expression will create the result that you are after.
In SSIS you would use the DT_STR or DT_WSTR cast to accomplish. A few examples are
(DT_STR, 4, 1252)YEAR(GETDATE()) +
RIGHT("0" + (DT_STR, 2, 1252)MONTH(GETDATE()), 2) +
RIGHT("0" + (DT_STR, 2, 1252)DAY(GETDATE()), 2)
or
(DT_WSTR, 4)YEAR(GETDATE()) +
RIGHT("0" + (DT_WSTR, 2)MONTH(GETDATE()),2) +
RIGHT("0" + (DT_WSTR, 2)DAY(GETDATE()), 2)
See the MSDN documentation for more information.
To add to the other answers, here is a reference guide I use frequently:
http://sqlis.com/sqlis/post/Expression-Date-Functions.aspx