MS SQL Convert a date format mmddyyy into another format

Hello experts,

i'm importing data from a webservice and i get a date in this format: "mmddyyyy hh:mm:ss", exe.: "04302015 11:45:01"

how can transform this: "mmddyyyy hh:mm:ss" into this: "dd-mm-yyyy hh:mm:ss", in transact SQL?
Exe.: "04302015 11:45:01" => "30-04-2015 11:45:01"

I tried several approaches with CAST and CONVERT but without success :(

Thx in advanced :)

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

You could use SUBSTRING() for this, for example:

SELECT(CONCAT(SUBSTRING("04302015 11:45:01",3,2), "-" ,SUBSTRING("04302015 11:45:01",1,2), "-" , SUBSTRING("04302015 11:45:01",5,4) , " " ,  SUBSTRING("04302015 11:45:01",10,8)))

Open in new window

justaphaseAuthor Commented:
i thought about that... but i also  thought that there was a simple way LOL...
Lokesh B RDeveloperCommented:

Replace the  double quotes with the single quotes in the above reply.

SELECT(CONCAT(SUBSTRING('04302015 11:45:01',3,2), '-' ,SUBSTRING('04302015 11:45:01',1,2), '-' , SUBSTRING('04302015 11:45:01',5,4) , ' ' ,  SUBSTRING('04302015 11:45:01',10,8)))

Open in new window

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
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

justaphaseAuthor Commented:
Yeah i knew that, lol

Vitor MontalvãoMSSQL Senior EngineerCommented:
Problem is that date could come with day and month separators and since it don't you'll need to add it before converting to date type:
SELECT CONVERT(DATETIME, STUFF(STUFF(@MyDate,3,0,'/'),6,0,'/'), 0)

Open in new window

NOTE: Replace @MyDate with the variable or field that you are working with.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Doesn't seem correct to accept a comment that it was only intended to correct some typo. At least you should share the points between the two Experts (Accept the 1st comment as Answer and the 2nd as Assist).
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
Microsoft SQL Server

From novice to tech pro — start learning today.