ssis convert data from source to destination - sql-server

I have the below data in an excel source:
Date: 1-Jun-15
Received: 10
Answered: 9
AvgWaitTime: 0:00:05
AvgHandleTime: 0:00:07
Abandoned: 1
The destination table is as below:
Date date,
Received float,
Answered float,
AvgWaitTime time(7),
AvgHandleTime time(7),
Abandoned float
When I am trying to import the data I am getting error in Input column date as sql is unable to convert it to date format. I surpassed this by creating a new table as per the suggestion given by sql. In the new table the data type for the date column is datetime. Then I got stuck in AvgWaitTime as is sql is unable to convert it to time format.
Is there a way to convert all the required column data from source and then put it to destination? Thanks in advance.

In the destination table you could change the type of the column date to string. It should work. Then you could use a derrived column placed in your task between the source and destination to change it back to date (type datetime, derrived column tab use replace). Untested code below:
> (DT_DATE)("20" +
> SUBSTRING([ReceivedDt], 1, 2) + "-" +
> SUBSTRING([ReceivedDt], 3, 2) + "-" +
> SUBSTRING([ReceivedDt], 5, 2))

Related

SSIS DT_STR to DT_DBDATE using Derived column

I have a fixed format .txt file which has a date field. This field may have a date or be blank. I have wracked my brains to get this field to convert from DT_STR to DT_DBDATE in the Derived Column transformation. I have tried multiple scenarios:
[CHNG_DT] == "" ? ISNULL((DT_DBDATE)[CHNG_DT] ) : (DT_DBDATE)SUBSTRING([CHNG_DT], 1, 4)) + "-" + (DB_DBDATE)(SUBSTRING(CHNG_DT], 6, 2)) + "-" + (DB_DBDATE)(SUBSTRING([CHNG_DT], 9, 2))
[CHNG_DT] == "" ? NULL(DB_DBDATE) : (DB_DBDATE)[CHNG_DT]
ISNULL([CHNG_DT]) ? NULL(DT_DBDATE) : (DT_DBDATE)((DT_STR, 10, 1252)[CHNG_DT])
ISNULL(CHNG_DT) ? NULL(DT_DBDATE) : (DT_DBDATE)((DT_STR,10,1252)CHNG_DT)
LEFT([CHNG_DT], 10) == " " ? ISNULL((DT_DBDATE)[CHNG_DT]) : (DT_DBDATE)[CHNG_DT]
(DT_DBDATE)CHNG_DT
First of all, when you are looking to convert values from DT_STR to DT_DBDATE, you should make sure that those values are stored in the yyyy-MM-dd format. You can refer to the following official documentation to learn more:
Converting Between Strings and Date/Time Data Types in SSIS
The second suggestion is to use the LTRIM() and RTRIM() functions to make sure that there are no white blanks in the beginning and end of the date values. As an example:
LTRIM(RTRIM(REPLACENULL([CHNG_DT],""))) == "" ? NULL(DT_DBDATE) : (DT_DBDATE)LTRIM(RTRIM([CHNG_DT]))
If date values are stored in a different format, you should write an expression that changes the format before converting it to DT_DBDATE. As an example:
How to convert string in format dd.mm.yyyy to date using SSIS expression?
Other helpful answers:
SSIS Source Format Implicit Conversion for Datetime
Convert DDMonYY and time to datetime column in SSIS package (Derived Column)
The year function doesn't support dt_wstr

wrong Dates values in ssis

I am working with ssis. I have a column that contains date in format integer YYYYMMDD: For example to day I get 20190214.I am using derived column to convert it in format date YYYY-MM-DD in my case for example 2019-02-14
But sometimes I receive wrong values for example 20188101. How could I test that I have good values to be converted to date?
There are 3 methods to achieve that
(1) Using Another Derived Column
Beside of the first Derived COlumn, you can add another derived column with the following expression:
(DT_DATE)(LEFT([DateColumn],4) + "-" + SUBSTRING([DateColumn],5,2) + "-" + RIGHT([DateColumn],2))
If the date value is incorrect redirect the bad from Error Output configuration , and you can handle these bad values from the Error Output.
Helpful Links
USING THE ERROR OUTPUT OF THE DERIVED COLUMN
How to prevent CAST errors on SSIS?
(2) Using a Script Component
Add a script component to check if the value is correct, and add a Output Column of type i.e. OutDateColumn, use a similar code (VB.NET)
IF Not Row.DateColumn_IsNULL Then
dim dtDate as DateTime
If DateTime.TryParseExact(Row.DateColumn,"yyyyMMdd",System.Globalization.InvariantCulture,System.Globalization.DateTimeStyles.None , dtDate ) Then
Row.outDateColumn = dtDate.ToString("yyyy-MM-dd")
Else
Row.outDateColumn_IsNull = True
End If
Else
Row.outDateColumn_IsNull = True
End If
(3) Add a Data Conversion Transformation
After the derived column Transformation, add a Data COnversion Transformation and try to convert the Date Derived Column you add to DT_DATE, if conversion failed, than handle the bad values from Error Output as explained in the first method.

Convert 12 hours to 24 hours from varchar column in SQL Server

In my existing SQL Server database, there is a column like this:
Column name and type: meeting_time varchar(22)
Values stored: 02:30:PM
Now I want to convert it into 24 hours format. i.e., it should convert to 14.30
When I tried this:
SELECT
CONVERT(VARCHAR(8), meeting_time, 1)
FROM
group_info
WHERE
MGI_Id = 1
It will return the same value which stored in this column. When I tried with converting it into time or datetime, it throws exception.
Can anyone please help me!
Thanks in advance.
This should work:
select convert(time, stuff(meeting_time, 6, 1, ' '))
Not sure if it works with all locales / languages, so you should check that first.
This assumes that your date is always in the same format, that the extra : is in the 6th character.

Convert a column that has a data type of integer into date in Cognos report studio

I have a column that appear like this and the data type is integer. I get that data from AS400 server that's why it uses integer data type. The date format is represent as YYYYMMDD
In report studio, I created a data item that would convert this integer column to date time. But it failed.
I have tried lots of different approach but none of these worked.
cast([WCPDOD], 'YYYYMMDD')
cast([WCPDOD], date)
UDA-SQL-0219 The function "to_date" is being used for local processing but
is not available as a built-in function, or at least one of its parameters is not supported.RSV-SRV-0042
cast([WCPDOD], YYYY-MM-DD)
cast([WCPDOD], datetime)
cast_timestamp([WCPDOD], datetime)
cast_timestamp([WCPDOD], date)
cast_integer([WCPDOD], date)
Can someone help me with this? My goal is to get this 20150729 into this 07/29/2015 at least
First, cast your 10-digit integer into a string:
Data Item2
cast([Data Item1],varchar(10))
Next, use substring to extract out the date components and build a date string:
Data Item3
substring([Data Item2],1,4) + '-' + substring([Data Item2],5,2) + '-' + substring([Data Item2],7,2)
Lastly, convert the resulting string to date format:
Data Item4
cast([Data Item3],date)
Of course, this can all be done in a single expression but I broke it out here for clarity.
In SQL Server, you can convert the integer field to varchar and then to a date and then use the date style 101 to achieve your desired format:
DECLARE #datevalue int = '20150729';
SELECT convert(varchar(10),cast(cast(#datevalue AS varchar(10)) as date), 101);

Date Format In SSIS Derived Column

I have 2 questions, I have a text file with all my data, in the text file I have for example Sply_DT and Imprt_DT.
For Sply_Dt I have to create getdate() and I have it formatted as 2012-10-25 12:04:16.09900000 using (DT_DBTIMESTAMP)(DT_DBDATE)GETDATE() but I want it formatted as MM-DD-YY.
And for Impt_DT, it's in the 5/16/2011 format in dataviewer but when I placed it into a table it looks like 2011-05-16 00:00:00.000 and I want it in MM-DD-YY format.
I think you have some confusion about the datetime data type. It does not care whether your locale is US (mm/dd/yyyy), Japan (yy/mm/dd) or the UK (dd/mm/yyyy), it will always be stored in the internal format.
If you don't like the default presentation, you can investigate SET DATEFORMAT and perhaps that makes sense for your query.
You can also apply the appropriate CONVERT format when you are querying the data to make it in your intended format.
DECLARE #datevar datetime = '2012-10-25'
SELECT CONVERT(char(10), #datevar, 10) AS YourFomat, #datevar AS defaultFormat
If I have misunderstood your question, please clarify.
An easier way to do it using the Derived Column component is simply like the following (for MM-DD-YY format):
LEN((DT_WSTR, 2)MONTH(GETDATE())) == 1 ? "0" + (DT_WSTR, 2)MONTH(GETDATE())) : (DT_WSTR, 2)MONTH(GETDATE())) + "-" + LEN((DT_WSTR, 2)DAY(GETDATE())) == 1 ? "0" + (DT_WSTR, 2)DAY(GETDATE())) : (DT_WSTR, 2)DAY(GETDATE())) + "-" + RIGHT((DT_WSTR, 2)YEAR(GETDATE()), 2)
As I understand it you're aiming to alter the datetime format of data coming from the text file. I recommend you use a derived column transform in a data flow task to either add a column or replace the existing column, then you can use more common .NET date operators and format strings within the derived column to first parse the date, then to convert it to a string with the given format. If that does not work, you can instead use a script component in the data flow task to do what I described, in which case you have access to .NET to perform your modifications.

Resources