Converting Date

  • Hi,

    I am not sure if this is possible but can you convert a string nvarchar(50) to a date.

    I need to convert 'May - 2016' to 2016-05-01

    Thanks in advance for the help.

    Paul

  • You could remove the "- " and put an "01 " at the beginning. You'd then be able to cast it as date, subject to your local language settings, of course.

    John

  • No need to remove the dash (-).

    DECLARE @cDate nvarchar(50) = 'May - 2016' ;

    SELECT CONVERT( date, '01 - ' + @cDate );

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks for the help. Both solutions worked.

Viewing 4 posts - 1 through 3 (of 3 total)

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