Date Format In SSIS Derived Column - sql-server

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.

Related

Date conversion in a XML column in SQL Server

How do I convert the below mentioned XML code date format.
<StartDate>2015-12-24T00:00:00</StartDate>
<EndDate>2015-12-29T15:39:20</EndDate>
If you're accessing your XML content with the built-in XQuery functionality, you can just use the .value() method and define the output datatype to be a DATETIME2(3) type - no special treatment necessary:
DECLARE #InputTbl TABLE (ID INT NOT NULL, XmlContent XML)
INSERT INTO #InputTbl (ID, XmlContent)
VALUES (1, '<Root>
<StartDate>2015-12-24T00:00:00</StartDate>
<EndDate>2015-12-29T15:39:20</EndDate>
</Root>');
SELECT
StartDate = XC.value('(StartDate)[1]', 'datetime2(3)'),
EndDate = XC.value('(EndDate)[1]', 'datetime2(3)')
FROM
#InputTbl
CROSS APPLY
XmlContent.nodes('/Root') AS XT(XC)
This returns this output:
There are several correct answers, but I've got the feeling, that these answers don't hit your actual issue:
In my table there is a xml column, and that xml column contains somuch data including date, And i want to update those dates to date format like '2/28/2017' now the date format is like '2012-04-26T00:00:00'
If I got you correctly you want to change the stored dates within your XML to another format, correct?
Simple answer: Don't!
ISO8601 is the standard format for date/time values within XML. The format you would like more 2/28/2017 is culture related and could lead to errors, or even worse!, to wrong values, if day and month both are below 13: 04/05/2017 can be taken as 4th of May or as 5th of April. You should never rely on culture settings!
XML is not meant to be human readable. Or in better words: It is meant to be human readable for technical people only... It is a standardizes string representation of structured, complex documents. The format of values should not bother you! Use an appropriate editor for the presentation.
This might be work
declare #date ='2015-12-24T00:00:00 2015-12-29T15:39:20'
SELECT CONVERT(date, Left(#date,10)) as NewDate
This may help you
DECLARE #X XML ='<StartDate>2015-12-24T00:00:00</StartDate>
<EndDate>2015-12-29T15:39:20</EndDate>'
SELECT #X.value('/StartDate[1]','DATETIME') AS START_DTE
,#X.value('/EndDate[1]','DATETIME') AS END_DTE
+-------------------------+-------------------------+
| START_DTE | END_DTE |
+-------------------------+-------------------------+
| 2015-12-24 00:00:00.000 | 2015-12-29 15:39:20.000 |
+-------------------------+-------------------------+
Update: From comments
The Datatime format in XML follows the ISO8601 standards. And you are thinking to format it native SQL format, which is not correct that you are treating XML like normal text data. The data present in XML format is correct. And If you want you can convert it to native SQL as above mentioned.
There is a good info at Wikipedia ISO 8601(Combined date and time representations)
on how the XML hold date time data.
A single point in time can be represented by concatenating a complete
date expression, the letter T as a delimiter, and a valid time
expression. For example, "2007-04-05T14:30".

ssis ETL dates into Excel

I'm using SSIS ETL (I'm a newbie) and trying to extract dates from a SQL server table in format "08/03/2013 00:00:00" to an Excel 2013 file. I need the dates in Excel to be in format "dd/mm/yyyy", they must remain as dates so that the hierarchy remains. ie when you do an autofilter on the column you can see the date groups. There are also some null entries in the column of data.
Things I have tried in ETL script:-
1)Direct conversion from SQL table to Excel file - Result "2013-03-08 00:00:00",yyyy/mm/dd ss:ss:ss no hierarchy. I have tried changing the Excel column format but no matter what I select the date stays the same.
2)I have then added a Date Conversion to the ETL, to change the SQL date to DT_DATE - Result "3/8/2013", m/d/yyyy, no hierarchy.
3)I have then tried a Date Conversion, to change the SQL date to DTW_STR - Result "2013/03/08 00:00:00", yyyy/mm/dd ss:ss:ss no hierarchy.
4)I have then tried various ETL conversions to strings but none are recognised by Excel as dates so no hierarchy.
I'm not in a position to "re-educate" users to a new date format of "yyyy/mm/dd"
I'm not a strong coder so would prefer to do this within ETL if possible.
So to summarise what I'm after I need the dates in Excel to be actual dates that can be grouped as such in format dd/mm/yyyy.
I believe what should do is try using the derived column transformation.
Derived column name: newDate
Derived column: Replace 'originalDate'
Expression: RIGHT("0" + (DT_WSTR, 2)DATEPART("dd", [originalDate]), 2) + "/" + RIGHT("0" + (DT_WSTR, 2) DATEPART("mm", [originalDate]), 2) + "/" + (DT_WSTR, 4)DATEPART("yyyy", [originalDate])
This will take each part and convert it into a string format so that we can concatenate them back together into the format we want. You may need to enclose everything in parenthesis and convert it back to date format if excel doesn't recognize it. (DT_DATE)(...)

Convert from 'YYYYMMDD' format to to SQL Datetime2

I have an issue concerning conversion in SSIS.
I'm trying to convert StartDATE from DT_WSTR to Datetime2 (for SQL Server)
My date originaly looks like this 20140804 but I need to convert it to Datetime2 in such format 2014-08-04 00:00:00.0000000.
What I've done earlier with the StartDATE Column is:
RTRIM(DATSTHYRA)
Since I need to remove blank spaces...
I figured I can use the already Derived Column and add a new expression to convert it to Datetime2 but I'm running into issues and can't really find a topic online that covers my issue.
You can do it in a single step.
Add Derived Column transformation - transform your YYYYMMDD string to YYYY-MM-DD with SUBSTRING functions and then - cast to DT_DBTIMESTAMP2 with scale needed. This would yield an expression like
(DT_DBTIMESTAMP2, 7)(SUBSTRING([StartDATE],1,4) + "-" + SUBSTRING([StartDATE],5,2)
+ "-" + SUBSTRING([StartDATE],7,2))
Then configure Error Output on this Derived Column transformation to capture and handle conversion errors.
In SSIS, you can use data conversion transformation, the data type mapping is database timestamp with precisionin SSIS is for datetime2 in SQL Server.

Perform string to date manipulation inside SSIS package with logical operators

There are 2 string columns on an inbound file, both formatted YYYYMMDD.
If column 1 is after 10/1/2015, add 90 days to the column 1 date and load in column 2. The data needs to be loaded in the system as YYYYMMDD string (so convert it back).
I'm trying to set up a Dervied Column module to do this, but I haven't been having much luck.
Here's what I have so far:
[Column 1] >= (DT_DATE)"2015-10-01" ? DATEADD("dd",90,[Column 1])
Please help.
There might be a simpler way of doing this, but since you are trying to do this with a derived column transform, this is how I did it for now.
Use a derived column transform to Convert column1 to date
Use a derived column transform to check condition and change value
Use a derived column transform to change it back to string
This is the code to change the YYYYMMDD string to date (I named it Column1AsDate)
(DT_DATE)(SUBSTRING(column1, 1, 4) + "-" + SUBSTRING(column1, 5, 2) + "-" + SUBSTRING(column1, 7, 2))
This is the code to check the condition (Now use Column1AsDate from the earlier transfor, I named this column as Column1AfterCondition)
Column1AsDate >= (DT_DATE)"2015-10-01" ? DATEADD("dd", 90, Column1AsDate) : Column1AsDate
This is the code to put it back to string (Using Column1AfterCondition from earlier transform)
(DT_WSTR, 4)DATEPART("yy", Column1AfterCondition) + RIGHT("0" + (DT_WSTR, 2)DATEPART("mm", Column1AfterCondition), 2) + RIGHT("0" + (DT_WSTR, 2)DATEPART("dd", Column1AfterCondition), 2)
Create a sample text file
This is the package
This is the data viewer after condition is checked
This is the data viewer after converting it back to string
So I suppose at the end, you can map the appropriate column to the destination. In this case the "Column1As String". Now that I think of it, I should have probably used better names.. but hopefully you get the picture.

SSIS - Converting unformatted strings dates to YYYY-MM-DD

I'm trying to convert the string column [mydate] to a date, and I would like to use the Derived Column Transformation. The problem is this that my dates are like '1/10/2015', '1/1/2015', '11/1/2015' and '11/9/2015'. So the format can change between D/MM/YYYY, D/M/YYYY, DD/M/YYYY, and DD/MM/YYYY.
Can you guide me in creating the expression for the Derived Column Transformation?
I tried to use something like this:
(DT_DATE)(ISNULL([mydate]) == FALSE ? (RIGHT([mydate],4) + "-" + "2" + "-" + LEFT([mydate],1)) : [mydate])
This task is quite tricky since we're not only dealing with unformatted String dates but they are also not in the region netural YYYY-MM-DD format.
To fix this, we can use the follwing expression for the Derived Column:
RIGHT([mydate],4) + "/" + SUBSTRING([mydate],FINDSTRING([mydate],"/",1) + 1,FINDSTRING([mydate],"/",2) - FINDSTRING([mydate],"/",1) - 1) + "/" + SUBSTRING([mydate],1,FINDSTRING([mydate],"/",1) - 1)
The expression is quite long but what it is doing is taking the values (month, day, year) between the forward slashes / and concatenating them into a format that resembles YYYY/MM/DD that can then be converted in SSIS using a Data Conversion transformation. This avoids the error of dealing with the change in length in dates like 1/1/2000 and 10/10/2000.
The output of the derived column was named YYYYMMDD and this value was then passed into a Data Conversion transformation that has output Date Converted YYYYMMDD as seen below.
The Data Conversion task is simply doing the follwing:
You can use Replace to solve this
Replace(ColumnName,"/","-")

Resources