Insert from Oracle into SQL Server datetime field

I have a web service that extracts data from an Oracle table and inserts into a SQL Server table. I'm getting the "String was not recognized as a valid datetime" error. Is there a way to get this to work? I'm using Visual Studio 2012, SQL Server 2008 R2 and Oracle 11g. Is there a fix I can use in the actual SQL statement that grabs the data? Thanks.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It sounds like you're not formatting the string correctly. In SQL Server there are a number of formats you can use that it will convert into date and time. By default you should try to format the incoming date like this:

1900-01-01 00:00:00

(YYYY-MM-DD hh:mm:ss)

Can you work out which records are causing the problem and show us the offending data?
How are you actually pushing the data into records? With a SQL statement in a language like VB.Net or C# ? If so, you might want to consider using a stored procedure in SQL Server instead, and coding it to check for good dates ... if it finds a problem it can divert the record to an errors table instead of your intended destination ...



(ref: here for date definition)
Could well be the ISO8601 format  (style number 126) being provided
maybe with with time zone Z (style number 127)


convert(datetime, your_data_here ,126)
convert(datetime, your_data_here ,127) -- if there is time zone info

CAST and CONVERT (Transact-SQL)

{+an edit}
Once you get what the correct input format you have, you can use this method to convert the Varchar to a DateTime value:
public static DateTime GetDateTime_From_String(String string_date, String format)
    return DateTime.ParseExact(string_date.Trim(), format, CultureInfo.InvariantCulture, DateTimeStyles.None);

Open in new window

The usage is:
String dateFromOtherDB = "2013-09-23 11:16:00";
DateTime typedDateValue = GetDateTime_From_String(dateFromOtherDB, "yyyy-MM-dd HH:mm:ss");

Open in new window

Please take a minute to look at this article:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dodgerfanAuthor Commented:
All of this helped me find what I needed. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.