Doubt with case

  • What would be the result of this expression??

    declare @month int

    set @month = -1

    select case @month

     when  -1 then datename(mm, cast(CAST(@month AS VARCHAR) + '-01-2004' as datetime))

     else 'test'

    end as month

    I'm getting

    Syntax error converting datetime from character string.

    Please explain

    Shiva

  • Hi shiva,

    what you are trying to do is append -1 to '-01-2004'  which results in '-1-01-2004' which is not a valid string that can be casted to date.

    I donot know your exact requirement. but if you can eliminate the minus(-) I think the syntax error will be rectified.

    declare @month int

    set @month = 12

    select case @month

     when  12 then datename(mm, cast(CAST(@month AS VARCHAR) + '-01-2004' as datetime))

     else 'test'

    end as month

    -Nagasree

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply