Friday, 27 September 2013

Convert date of format dd mon yyyy to return integer of month in SQL

Convert date of format dd mon yyyy to return integer of month in SQL

I have a varchar variable containing value 01 May 2013 and I need to
obtain the integer of the Month part.
for eg if 01 May 2013 is input I should get the result as 5
The query I wrote was : select
DATEPART(MM,CONVERT(DATETIME,CONVERT(VARCHAR(15),'01
'+SUBSTRING(FISCAL_MONTH,1,3)+' 2013'),100)) FROM
Here FISCAL_MONTH is my column name from the table. However this query is
showing me the result, for eg 11 for November but is also throwing the
following error : Msg 241, Level 16, State 1, Line 1 Conversion failed
when converting date and/or time from character string.
I have tried various combinations, bit in vain. Kindly note I need it in a
query. Kindly help.

No comments:

Post a Comment