question

anandgu avatar image
anandgu asked

I have to write this date to handle my query

Date = Dec.24,2015 For example if substring(price_dt,1,3)='Dec' then 12+'-'+substring(price_dt,5,6 How to write this in an expression like above my expression.
sql-server-2008sql-server-2012format
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
You have some good answers below. This web site runs by you voting and marking the best answers. For each helpful answer below, indicate this by clicking on the thumbs up symbol next to those answers. If any one answer lead to a solution, indicate this by clicking on the check mark next to that answer. This helps indicate your solution for anyone else with the same question.
0 Likes 0 ·
Gazz avatar image
Gazz answered
very similar to this question: [ https://ask.sqlservercentral.com/questions/92630/need-to-get-date-and-time-in-format-of-20120817133.html][1] Here is what you can do (you will have to finish of the CASE, I couldn't be bothered, sorry): DECLARE @TEST AS CHAR(11) SET @TEST = 'Dec.24,2015' SELECT @TEST SELECT RIGHT(@TEST, 4) , SUBSTRING(@TEST, 5, 2) , CASE WHEN LEFT(@TEST, 3) = 'JAN' THEN 1 WHEN LEFT(@TEST, 3) = 'FEB' THEN 2 ..... WHEN LEFT(@TEST, 3) = 'DEC' THEN 12 END, CONVERT(DATE, RIGHT(@TEST, 4) + CASE WHEN LEFT(@TEST, 3) = 'JAN' THEN '1' WHEN LEFT(@TEST, 3) = 'FEB' THEN '2' .... WHEN LEFT(@TEST, 3) = 'DEC' THEN '12' END + SUBSTRING(@TEST, 5, 2)) [1]: https://ask.sqlservercentral.com/questions/92630/need-to-get-date-and-time-in-format-of-20120817133.html
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

anandgu avatar image anandgu commented ·
Thanks you so much GAZZ
0 Likes 0 ·
GPO avatar image
GPO answered
Try this: print cast(replace('Dec.24,2015','.',' ') as date)
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gazz avatar image Gazz commented ·
That is way better than my method
1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.