Solved

SSIS: string to smalldatetime

Posted on 2010-08-30
5
1,520 Views
Last Modified: 2013-11-10
I have a date & timestamp loading in from a text file (YYYYMMDDHHMMSS) that I need to convert to a dbtimestamp and match against a smalldatetime field in a table.

I'm loading the data in as a string and then via the Derived column tool I do a DT_DBTEIMESTAMP type cast (into the format as I see it in a sql statement YYYY-MM-DD HH:MM:00), but the process keeps erroring out at that step. Is the internal format different (MMDDYYYHHMMSS) or what am I missing?

Thanks!
0
Comment
Question by:wppiexperts
  • 3
5 Comments
 

Author Comment

by:wppiexperts
Comment Utility
As an additional note: the type cast format I'm using is:
(DT_DBTIMESTAMP)(SUBSTRING(IntervalStartDateTime,5,2) + "-" + SUBSTRING(IntervalStartDateTime,7,2) + "-" + SUBSTRING(IntervalStartDateTime,1,4) + " " + SUBSTRING(IntervalStartDateTime,9,2) + "-" + SUBSTRING(IntervalStartDateTime,11,2) + ":00")

and this fails during execution.

If I remove the portion of the datatype that represents time (SUBSTRING(IntervalStartDateTime,9,2) + "-" + SUBSTRING(IntervalStartDateTime,11,2) + ":00") then this step works fine, so am I not building the datetime value properly?
0
 
LVL 21

Expert Comment

by:huslayer
Comment Utility
1-Why would you set 50 points to the question while you're a Premium Service Member and have no limit on points !

2-what's the error msg you're getting? the answer should be obvious in the error msg.

3-DT_DBTEIMESTAMP Cast will cast from "date" to DT_DBTEIMESTAMP, and you're not sending "date" type in the pipe!

4-Try a Derived COLUMN Transfer and type this to convert the YYYYMMDD to YYYY-MM-DD
in the expression you must have

Derived Column name Derived column Expression
Date  <Replace Date> SUBSTRING ([DATE] , 1 , 4) + "/" +SUBSTRING ([DATE] ,5 , 2) + "/" +SUBSTRING ([DATE] ,7 , 2)



5-then cast the above to DT_DBTEIMESTAMP


so the final query should be:

(DT_DBTIMESTAMP)(SUBSTRING ( [Stmt From]  , 1 , 4) + "/" +SUBSTRING ([Stmt From] ,5 , 2) + "/"+ SUBSTRING ([Stmt From] ,7 , 2))


hope that helps :)
0
 

Author Comment

by:wppiexperts
Comment Utility
thats what I'm finding a bit confusing about the conversion process. If my original value was "YYYYMMDDHHMMSS" and I build an expression in the Derived column transformation in the format "YYYY-MM-DD HH:MM:00" it fails using the DT_DBTIMESAMP, but if I remove the time reference, it works fine. I was under the assumption that if I passed the function a valid datetime in the proper format, it would change it from a string to a datetime datatype.

so, I guess my underlying questions are:
1. How do I use the derived column transformation to take a string and create a datetime value
2. Based on the expression I posted above, what am I doing wrong in that it doesn't recognize it as a datetime?

0
 
LVL 30

Accepted Solution

by:
Reza Rad earned 50 total points
Comment Utility
what about this one:

(DT_DBTIMESTAMP)(SUBSTRING(IntervalStartDateTime,5,2) + "-" + SUBSTRING(IntervalStartDateTime,7,2) + "-" + SUBSTRING(IntervalStartDateTime,1,4) + " " + SUBSTRING(IntervalStartDateTime,9,2) + ":" + SUBSTRING(IntervalStartDateTime,11,2) + ":00")

try it.


0
 

Author Closing Comment

by:wppiexperts
Comment Utility
perfect!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now