SSIS - cast a date to use in an expression - sql-server-2005

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

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] + "'"

Changing Date format in SSIS expression

In SSIS, I have an expression that goes:
"SELECT _sessions.session_id AS session_id, _sessions.user_id AS user_id,
_sessions.updated_at FROM public._sessions _sessions
WHERE _sessions.updated_at > '" +(DT_STR, 20, 1252)#[User::MaxId] + "'"
This outputs:
SELECT _sessions.session_id AS session_id, _sessions.user_id AS user_id,
_sessions.updated_at FROM public._sessions _sessions
WHERE _sessions.updated_at > '13/10/2016 11:58:00'
However I need the date to output as so: > '13-10-2016 11:58:00'. it's the dashes and not the slashes.
How do I change my expression in order to meet this?
In SSIS, when you cast a DateTime type to string, it's calling the underlying .NET object's ToString() method. That will use your localization rules to produce the string.
You have dd/mm/yyyy hh:mm:ss due to a UK localization. I'd have mm/dd/yyyy etc for a US locale.
Therefore, if you want a special format, you have to build it yourself. I'm using StartTime as it made it easier for testing but, assuming MaxId is also a datetime type, it's a simple substitution
(DT_WSTR, 4) YEAR(#[System::StartTime])
+ "-"
+ RIGHT("0" + (DT_WSTR, 2) MONTH(#[System::StartTime]),2)
+ "-"
+ RIGHT("0" + (DT_WSTR, 2) DAY(#[System::StartTime]),2)
+ " "
+ LEFT(RIGHT((DT_WSTR, 24) (#[System::StartTime]), 11), 9)
The thing to note above is that to get the leading zero for day and month, we prepend a leading zero to the emitted string (for the month of March, we'll have 03, for October, it will be 010) and then only preserve the trailing 2 characters. This is much cleaner to troubleshoot than conditional logic based on the actual date.
I take a lazy route for time manipulation so I use the last 11 characters of the default time cast to get the time portion but then strip the AM/PM modifier off the end of that.
You have SSIS non-standard format for date DD-MM-YYYY. Try this expression
RIGHT("0" + (DT_STR, 2, 1252)DATEPART("dd", #[User::MaxId]), 2) + "-" +
RIGHT("0" + (DT_STR, 2, 1252)DATEPART("mm", #[User::MaxId]), 2) + "-" +
(DT_STR, 4, 1252)DATEPART("yyyy", #[User::MaxId]) +
+ " " + (DT_STR, 8, 1252)(DT_DBTIME)#[User::MaxId]
This builds your date. Second line adds time part.

SSIS Wildcard Flat file connection

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.

Add characters in the variable

I need to make the string/varchar variable with value 20061212 to be string/varchar value 2006-12-12 00:00:00
I can't find the right SQL syntax code to make it happened in SQL Server.
The code can be created as stored procedure or similar in SQL Server.
SELECT
LEFT(#string, 4)
+ '-' + SUBSTRING(#string, 5, 2)
+ '-' + SUBSTRING(#string, 7, 2)
+ ' 00:00:00'
You could cast your string to a DATETIME and then convert it back to a string in your chosen format. But that would likely be slower than this more explicit option.
SELECT
CONVERT(VARCHAR(19), CAST(#string AS DATETIME), 120)
You can use stuff
declare #s varchar(8) = '20061212'
select stuff(stuff(#s, 7, 0, '-'), 5, 0, '-')+' 00:00:00'
You can use the SUBSTRING() function (detailed here: http://www.sql-statements.com/sql-substring.html) to take apart the original string, and then put the pieces back together with the added dashes & time at the end using the concatenation operator +.
Culture independent variant:
SELECT CONVERT(varchar, convert(datetime, '20061212', 112), 120)
BUT this only valid for DATETIME-allowed range of dates, use DATETIME2 instead