Using SSIS 2013, I have a table which has an invoice date stored as an integer in the format of YYYYMMDD and I need to determine the beginning of the month date and the end of the month date based upon the invoice date. Both the beginning and end dates will also be stored as integers.
For example my invoice date is: 20170512
and I want to return:20170501 (BeginDate)
and 20170531 (EndingDate)
You can add Two Derived COlumns with the following expressions"
These Expression will return 2 Columns of type DT_DATE
First Day:
(DT_DATE)(LEFT([InvoiceDate],4) + "-" + SUBSTRING([InvoiceDate],5,2) + "-01")
Last Day:
DATEADD("d",-1,DATEADD("m",1,(DT_DATE)(LEFT([InvoiceDate],4) + "-" + SUBSTRING([InvoiceDate],5,2) + "-01")))
If you need to return 2 columns with the same format YYYYMMDD
First Day:
LEFT([InvoiceDate],4) + "-" + SUBSTRING([InvoiceDate],5,2) + "-01"
Last Day:
LEFT([InvoiceDate],4) + "-" + SUBSTRING([InvoiceDate],5,2) + RIGHT("0" + (DT_WSTR,2)DAY(DATEADD("d",-1,DATEADD("m",1,(DT_DATE)(LEFT([InvoiceDate],4) + "-" + SUBSTRING([InvoiceDate],5,2) + "-01")))),2)
Related
I'm receiving my dates in a MMddyyyy and a Mddyyyy format in one source, if the month has 1-9 digit the month is 1 digit, if the month is between 10-12 its 2 digits, I need an SSIS expression that will be able to convert them into a date format.
ex 2051994 or 12051994
both from the same source but they are hard to convert with the same SSIS expression
The cleanest way to do it would be to normalise the input format and then convert it to data as usual to the format of your choice. In order to normalise the input, i.e. make it the same length for every month, you can add a Derived Column transformation with this expression:
RIGHT("0" + yourinputdate,8)
if the input date is integer instead of string you can do this:
RIGHT("0" + (DT_STR,8,1252)(yourinputdate),8)
using the above expressions 12051994 will remain unchanged but 2051994 will be converted to 02051994.
from this transformation downstream you can convert the string to the format you prefer, or you can do it all in the same transformation although it would make the expression a bit unreadable.
You can use the following expression to convert Mddyyy and MMddyyyy string into DateTime value:
(DT_DATE)(RIGHT("0000" + RIGHT(RIGHT("0" + [DateColumn],8),4),4) +
"-" + RIGHT("00" + LEFT(RIGHT("0" + [DateColumn],8),2),2) +
"-" + RIGHT("00" + SUBSTRING(RIGHT("0" + [DateColumn],8),3,2),2))
Hi I have one doubt in ssis
I want change date format M/d/yyy hh:mm:ss to yyyy-MM-dd using ssis expression and DateFormat variable datatype is string and getting format like 5/28/2019 12:00:00 AM. Data comes like below:
5/28/2019 12:00:00 AM
Based on above values i want output in other variable
2019-05-28
I tried like expression
(DT_STR, 4, 1252) DATEPART("yy" , #[User::DateFormat]) + "-" +
RIGHT("0" + (DT_STR, 2, 1252) DATEPART("mm" , #[User::DateFormat]), 2) +
"-" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("dd" , #[User::DateFormat]), 2)
but getting error like below :
ADDITIONAL INFORMATION:
The function "YEAR" does not support the data type "DT_WSTR" for
parameter number 1. The type of the parameter could not be implicitly
cast into a compatible type for the function. To perform this
operation, the operand needs to be explicitly cast with a cast
operator.
Evaluating function "YEAR" failed with error code 0xC0047089.
The function "YEAR" does not support the data type "DT_WSTR" for
parameter number 1. The type of the parameter could not be implicitly
cast into a compatible type for the function. To perform this
operation, the operand needs to be explicitly cast with a cast
operator.
can you please tell me how to write expression to achive this task in ssis
The main error is that #[User::DateFormat] variable is a string variable while DATEPART() function can be only applied to a date time value. (Even if the string variable stores a date time value)
You can benefit from TOKEN() function to achieve that:
TOKEN(TOKEN( #[User::DateFormat],"/",3)," ",1) + "-" +
RIGHT("0" + TOKEN( #[User::DateFormat],"/",2),2) + "-" +
RIGHT("0" + TOKEN( #[User::DateFormat],"/",1) ,2)
I have added type casts for date.
This would help
(DT_WSTR, 4) YEAR( (DT_DATE) #[User::Variable] ) + "-" + RIGHT("0"+(DT_WSTR,2) MONTH( (DT_DATE) #[User::Variable] ),2) + "-" + RIGHT("0"+(DT_WSTR, 2) DAY( (DT_DATE) #[User::Variable] ),2)
Create a new variable in your SSIS package with data type String and use the datetime field(here as #[User::Variable]) coming in your input in that variable as in below expression:
(DT_WSTR, 4) YEAR( #[User::Variable] ) + "-" +
RIGHT("0"+(DT_WSTR,2) MONTH( #[User::Variable] ),2) + "-" +
RIGHT("0"+(DT_WSTR, 2) DAY( #[User::Variable] ),2)
You can convert this string variable into date type later on if required.
I'm trying to convert an input of DT_WSTR to DT_TIMESTAMP on SSIS Derived column and when I do it the values do not match up, on one ocasion it recognizes the year correctly but on the other it think the day is the year
I tried to do
(DT_TIMESTAMP)((DT_WSTR,4)YEAR([Data Hora]) + RIGHT("0" + (DT_WSTR,2)MONTH([Data Hora]),2) + RIGHT("0" + (DT_WSTR,2)DAY([Data Hora]),2) + (DT_WSTR,10) Time_Column)
with no success
08-04-19 15:31:27 this is what I input and sometimes it recognizes the 19 as 2019 correctly but other times it thinks the 08 is the year so it puts 19/04/2008, I want the first one.
I expect to get the date correctly in dd/mm/yyyy hh:MM:ss.
Your question is not clear. In this answer I will try to fix the provided expression. Try using the following expression:
(DT_DBTIMESTAMP)((DT_WSTR,4)YEAR([Data Hora]) + "-" +
RIGHT("0" + (DT_WSTR,2)MONTH([Data Hora]),2) + "-" +
RIGHT("0" + (DT_WSTR,2)DAY([Data Hora]),2) + " " +
(DT_WSTR,10) [Time_Column])
Another suggestion is that, in cases where the date format is tricky (2 digit year part), it is better to use a script component to parse the date value since it provides more options using Date.ParseExact() function, as example:
Row.outDateColumn = DateTime.ParseExact(Row.inStringDate,"dd-MM-yy HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
I'm trying to format a datestamp to have leading zeros in an expression using SSIS 2008 R2.
My desired result would be Exceptions - YYYYMMDDHHMMSS.xls so as an example, now would be:
\\SomePath\Exceptions - 20150211155745.xls
I am having an issue adding the leading zeros to the day and month though.
I've tried the following expressions by trying to convert to DT_WSTR with the length set as well as picking the date apart usg SUBSTRING:
#[User::XLFileRootDir] + "Exceptions - " + (DT_WSTR, 4) DATEPART("YYYY", GETDATE()) + (DT_WSTR, 2) DATEPART("MM", GETDATE()) + (DT_WSTR, 2) DATEPART("DD", GETDATE()) + ".xls"
This results in \\SomePath\Exceptions - 2015211155745.xls (notice the missing leading zero on the month).
#[User::XLFileRootDir] + "Exceptions - " + (DT_WSTR, 4) SUBSTRING(GETDATE(), 1, 4) + ".xls"
This results in an error as the data type DT_DBTIMESTAMP isn't supported by the function SUBSTRING. I'm aware that some sort of conversion needs to take place but can't find a function within SSIS to complete this.
Could anyone help me with how to format the expression with leading zeros?
The problem you're running into is that the YEAR/MONTH/DAY functions return an integer. An integer won't present leading zeros. Therefore, the "trick" is to convert it to a string. Prepend a leading zero to that string. Then, shear off the last 2 characters using the RIGHT function. The trimming is only required for October, November, and December but the logic is cleaner to unconditionally apply RIGHT.
This builds your YYYYMMDD string.
(DT_WSTR, 4)YEAR(#[System::StartTime])
+ RIGHT("0" + (DT_WSTR, 2) MONTH(#[System::StartTime]), 2)
+ RIGHT("0" + (DT_WSTR, 2) DAY(#[System::StartTime]), 2)
I find it better to use the variable System::StartTime rather than GETDATE(), especially when time is involved. GETDATE will be evaluated each time it is inspected. Over long running packages, there can be a sizable drift in the values returned. System::StartTime is the time the package itself began. It is constant for the run itself but obviously resets per run.
For my application, I needed the exact current time, and I needed it down to the second, which yielded this:
(DT_WSTR, 4)DATEPART("yyyy",GETDATE())
+ RIGHT("0" + (DT_WSTR, 2) DATEPART("mm",GETDATE()),2)
+ RIGHT("0" + (DT_WSTR, 2) DATEPART("dd",GETDATE()),2)
+ RIGHT("0" + (DT_WSTR, 2) DATEPART("Hh",GETDATE()),2)
+ RIGHT("0" + (DT_WSTR, 2) DATEPART("mi",GETDATE()),2)
+ RIGHT("0" + (DT_WSTR, 2) DATEPART("s",GETDATE()),2)
SUBSTRING((DT_WSTR, 29) GETDATE(), 6, 2)
I have this string which is a combination of date and time but in string format meaning that it has no space. I parsed it from a very long string but I now have or need to convert it to a standard date and time.
This is my string for date and time:
141007024755
This is how I parsed it from a very long string of data
[date&time] = SUBSTRING(#ProductCode, 27, 12)
This is the format I'm expecting but can't do it.
2014-10-07 02:47:55.000
Can anyone give me a hint on how to do this? An advice perhaps.
Thanks.
If the string is always 12 characters long you could try this:
select cast(stuff(stuff(stuff('141007024755', 7,0,' '), 10,0,':'), 13,0,':') as datetime)
Basically it uses the stuff function to insert a space between the date and time parts and colons between the different time parts producing a string like141007 02:47:55that can be converted todatetimeusingcast.
Assuming the dates are formatted exactly as your sample string, you can just keep chopping up the string and appending it back together and cast the result to date. Simple function like this may help:
CREATE FUNCTION dbo.udf_ReturnDateFromString(#DateString AS VARCHAR(14))
RETURNS DATETIME
AS
BEGIN
SET #DateString = '20' + #DateString
RETURN CAST(LEFT(#DateString, 4) + '-' +
SUBSTRING(#DateString, 5, 2) + '-' +
SUBSTRING(#DateString, 7, 2) + ' ' +
SUBSTRING(#DateString, 9, 2) + ':' +
SUBSTRING(#DateString, 11, 2) + ':' +
SUBSTRING(#DateString, 13, 2) AS DATETIME)
END;
GO
SELECT dbo.udf_ReturnDateFromString('141007024755');
The result is:
2014-10-07 02:47:55.000
You get the idea. This was done on SS 2008 R2.