Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

converting an date from oracle to a regular datetime in sql Expand / Collapse
Author
Message
Posted Wednesday, July 17, 2013 4:20 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, March 24, 2014 10:07 AM
Points: 13, Visits: 70
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?

Post #1474831
Posted Wednesday, July 17, 2013 4:29 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:14 PM
Points: 2,763, Visits: 5,908
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1474832
Posted Wednesday, July 17, 2013 4:31 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: 2 days ago @ 4:43 AM
Points: 756, Visits: 631
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
Post #1474833
Posted Wednesday, July 17, 2013 4:51 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, March 24, 2014 10:07 AM
Points: 13, Visits: 70
Thanks guys - those both worked! Thanks for the help!
Post #1474836
Posted Wednesday, July 17, 2013 5:25 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:14 PM
Points: 2,763, Visits: 5,908
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1474839
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse