0

I want to convert string time '141312110' to this: '14:13:12.110'

Actually, I have bigint column has data like '20151110182425000' I want to convert them to datetime format like

'10-11-2015 18:24:25.000'  

I divided them to date and time to get date part used this :

Convert(varchar(10), convert(datetime, '20151110'),105)

Result : '10-11-2015'

And needed to get time part like that.

5
  • 2
    What DBMS are you using (SQL Server, MySQL, Oracle, something else)? And what version of the DBMS are you using? The answer will be very different depending on that. Commented Nov 10, 2015 at 16:43
  • 1
    Well it's the same as getting the date part, you convert to varchar and parse the string. What part are you stuck on? Commented Nov 10, 2015 at 16:44
  • 1
    Added sql-server tag based on the usage of convert() and datetime Commented Nov 10, 2015 at 19:37
  • @tab alleman time part not getting with the same way cause convertting datetime, time format not mach. Commented Nov 11, 2015 at 6:10
  • @siyual using MSSQL 2008 Commented Nov 11, 2015 at 6:23

1 Answer 1

1

You can turn your original string into the format "YYYY-MM-DDTHH:MM:SS.SSS'. This should be recognized as a datetime, regardless of internationalization settings.

And you can do this with a bunch of stuff()s:

select cast(stuff(stuff(stuff(stuff(stuff(cast(val as varchar(255), 15, 0, '.'
                                         ), 13, 0, ':'
                                   ), 11, 0, ':'
                             ), 9, 0, 'T'
                       ), 7, 0, '-'
                 ), 5, 0, '-'
             as datetime)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes i also did this but i need dd-mm-YYYY hh:mm:ss, and can't do that using the stuff or i did not discover the way of doing this with stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.