1

I'm getting date as string from Java output as: Wed Apr 27 09:56:56 CEST 2016

I would like to manually avoid typing "2016-04-2..etc" in my datetime field in my table during debug, and also avoid to create manual method for formating that date.

Instead, I would like to do this on the fly on database. I would like to convert given string format to date. I have tried many examples, but I couldn't solve it. Most close I found was this:

select convert(datetime, 'Wed Apr 27 09:56:56 CEST 2016', 127) from mytable

I need this in SQL Server 2012. Seems due to documentation (https://msdn.microsoft.com/en-us/library/ms187928.aspx), it can parse may formats, but it can't parse this kind of string I noticed.

Anybody knows how to convert this given string to date?

3 Answers 3

3
 select convert(datetime, REPLACE((SUBSTRING('Wed Apr 27 09:56:56 CEST 2016', PATINDEX('%CEST%', 'Wed Apr 27 09:56:56 CEST 2016')+5, 4)
 + ' ' + SUBSTRING('Wed Apr 27 09:56:56 CEST 2016', 5, (PATINDEX('%CEST%', 'Wed Apr 27 09:56:56 CEST 2016')-5))),'CEST',''), 109)
Sign up to request clarification or add additional context in comments.

Comments

3

Please try this below query:

 select cast(substring(replace ('Wed Apr 27 09:56:56 CEST 2016','CEST','') ,4,len(replace ('Wed Apr 27 09:56:56 CEST 2016','CEST',''))) as datetime)

Comments

2

If the format is fixed, this query should work

SELECT CONVERT(DATETIME,REPLACE(SUBSTRING('Wed Apr 27 09:56:56 CEST 2016',4, LEN('Wed Apr 27 09:56:56 CEST 2016')-3),'CEST','')) from mytable

Comments

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.