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.
Related
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]
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] + "'"
My Filename column looks like 'D181115.T000000'. I used the following code to make it a string, looks like '2018-11-15'.
select '20' + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)
from table_name
Then I want to convert the string to date type (because I need to sort by date)
select convert(datetime, '20 + substring(filename, 2,2) + '-' + substring(filename, 4,2) + '-' + substring(filename,6,2)')
from table_name
Then I got this error message:
The data types varchar and varchar are incompatible in the subtract
operator.
Any help will be greatly appreciated!
I suspet the database is SQL Server. In that case one can use just
select cast('20' + substring('D181115.T000000', 2,6) as date)
or
select try_cast('20' + substring('D181115.T000000', 2,6) as date)
YYYYMMDD is one of the two unambiguous date formats. The other is the full ISO8601 date+time format. YYYY-MM-DD on the other hand depends on the DATEFORMAT setting
Update
I'd suggest performing this conversion as part of data loading though. Applying functions to a field prevents the server from using any indexes that cover the field. The server will have to scan the entire table in order to produce the final values used for filtering and sorting.
At least consider addd an indexed computed column that produces the file date
I want to convert the string to date type
Look at the first and 2nd statements you included, they are different in that you are missing a quote and you added an extra quote in the second one.
declare #filename varchar(20) = 'D181115.T000000'
select convert(datetime, '20' + substring(#filename, 2,2) + '-' + substring(#filename, 4,2) + '-' + substring(#filename,6,2))
Produces output:
2018-11-15 00:00:00.000
I have the following two columns:
StartDate = 2017-01-01 00:00:00.000
EndDate = 2017-01-01 05:45:00.000
I need to write an SSIS expression for my derived column that will calculate the time between these two datetimes. Output should be:
05:45:00.0000000
Can anyone help with writing this expression?
Thanks in advance!!
You can use DATEDIFF() function to get the difference between two dates.
difference in Hours
DATEDIFF("Hh",[StartDate],[EndDate])
difference in minutes
DATEDIFF("mi",[StartDate],[EndDate])
difference in minutes
DATEDIFF("ss",[StartDate],[EndDate])
Suggested Expression to return HH:mm:ss
You have to get the difference in seconds then use the following expression
RIGHT("000" + (DT_WSTR,3)(DATEDIFF("ss",#[User::StartDate],#[User::EndDate]) / 3600),3) + ":" + RIGHT("00" + (DT_WSTR,2)((DATEDIFF("ss",#[User::StartDate],#[User::EndDate]) % 3600) / 60) ,2) + ":" + RIGHT("00" + (DT_WSTR,2)(DATEDIFF("ss",#[User::StartDate],#[User::EndDate])% 60),2)
References
DATEDIFF (SSIS Expression)
Convert Seconds to HH:MM:SS using SSIS
SSIS Expression – Convert Seconds to HHH:MM:SS Format
There is no direct function that gives you the expected output, you have to get the difference between both dates in the minimal unit you want (seconds or milliseconds) then you should build your own expression that convert it to HH:mm:ss format)
You can use the following expression to get the difference between two dates:
RIGHT("00" + (DT_WSTR,10)(DATEDIFF("ss",#[User::StartDate],#[User::EndDate]) / 3600),2) + ":" +
RIGHT("00" + (DT_WSTR,10)((DATEDIFF("ss",#[User::StartDate],#[User::EndDate]) % 3600) / 60) ,2) + ":" +
RIGHT("00" + (DT_WSTR,10)(DATEDIFF("ss",#[User::StartDate],#[User::EndDate])% 60),2)
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