Challenge to change source date formats to desired date formats - sql

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]

Related

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

calculate time difference between two dates in ssis

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)

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.

SQL generate string from splitting another

I have a number of corrupt Dates in an Advantage database, but luckily I have a reverse date field which is intact.
I need to use SQL to recreate the Date field in the form 'DD/MM/YYYY' from the RDate string 'YYYYMMDD'.
Something equivalent to:
UPDATE table SET Date = FormatDate('DD/MM/YY',StrToDate('YYYYMMDD',RDate))
I don't know if this is possible to select character positions and use variables this way in SQL or if I'll have to write a program (in Delphi) to do the operation.
Thanks
Try something like
update table set
date = cast(substring(rdate, 5, 2) + '/' +
right(rdate, 2) + '/' +
left(rdate, 4) as date)
/* where ... */
(assuming your connection's date format setting is the default 'MM/DD/YYYY')
TOndrej I'm not sure if it was a syntax error or if cast is not recognised but I achieved the desired result with the following SQL:
update members set "Expiry Date" = ((substring(RExpiryDate,7,2)) + '/' + (substring(RExpiryDate,5,2)) + '/' + (substring(RExpiryDate,1,4)))
Thanks for your help

Computed column

In current table I have a column that holds the date field in ddmmyyyy format and it is of type varchar(8). The column has some string value also. I want to create a computed column that will hold the value in DateTime format if the value in source column is valid date time.
Assuming your varchar(8) column is dateString :
Cast([dateString] as datetime)
SQL Server prefers dates in the format of yyyymmdd so there will be some string manipulation involved to format your data like this. We should also use the IsDate function to make sure we have a valid date.
So:
Cast(Case When IsDate(Right(#Data, 4)
+ SubString(#Data, 3, 2)
+ Left(#Data, 2)) = 1
Then Right(#Data, 4)
+ SubString(#Data, 3, 2)
+ Left(#Data, 2) End As DateTime)
Notice that this code should correctly handle invalid dates contained within your varchar column. If a date is invalid, this code will return NULL.
try parsing your varchar into dd/mm/yyyy before attempting the cast:
cast(substring([datestring],1,2) + '/' +
substring([datestring],3,2) + '/' +
substring([datestring],5,4) as datetime)