Solved

SSIS: string to smalldatetime

Posted on 2010-08-30
5
1,565 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
ID: 33560319
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:Jason Yousef, MS
ID: 33560419
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
ID: 33560813
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
ID: 33561548
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
ID: 33562128
perfect!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

863 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

23 Experts available now in Live!

Get 1:1 Help Now