SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


converting an date from oracle to a regular datetime in sql


converting an date from oracle to a regular datetime in sql

Author
Message
smrobin
smrobin
SSC Veteran
SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)

Group: General Forum Members
Points: 242 Visits: 136
So I have some data that I am importing in from Oracle - and the date is coming in the following way:

25-JUN-13 12.01.15.096000000 AM

If I run the following:

select convert(date,'25-JUN-13 12.01.15.096000000 AM') as [date]

I get the following error:
Conversion failed when converting date and/or time from character string


If I run the following:

select convert(datetime,substring('25-JUN-13 12.01.15.096000000 AM',0,10))

It returns the data in the format: 2013-06-25 00:00:00.000

I need to be able to grab the complete time stamp and put in a datetime format.


Any ideas on how to do that?
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43509 Visits: 19861
Like this?
SELECT CONVERT( datetime2, STUFF(STUFF(OraDate,13,1,':'),16,1,':'))
FROM (SELECT '25-JUN-13 12.01.15.096000000 AM' AS OraDate) O




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Erland Sommarskog
Erland Sommarskog
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5528 Visits: 875
DECLARE @oracledate varchar(50) = '25-JUN-13 12.01.15.096000000 AM'
select try_convert(datetime2, substring(@oracledate, 1, 12) + ':' +
substring(@oracledate, 14, 2) + ':' +
substring(@oracledate, 17, 50))



But this will hot work of if there is not leading zeores for all one-digit number.

And one could ask why Oracle produces this value in the first place. Surely Oracle has other options?

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
smrobin
smrobin
SSC Veteran
SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)

Group: General Forum Members
Points: 242 Visits: 136
Thanks guys - those both worked! Thanks for the help!
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43509 Visits: 19861
If there are no leading zeroes for one digit day, here's an alternative.
SELECT CONVERT( datetime2, PARSENAME(OraDate,4) + ':' + PARSENAME(OraDate,3) + ':' + PARSENAME(OraDate,2) + '.' + PARSENAME(OraDate,1))
FROM (SELECT '25-JUN-13 12.01.15.096000000 AM' AS OraDate) O




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search