Search code examples
sql-server-2008data-conversiondateadd

Convert a part of text to number and add it to a date in T-SQL


I am wondering how this can be done in t-sql. I need to extract the first two letters from a text field, convert it into number and add it to a date.

For example, if the text field is 20D, I have to take the first 2 letters from it, convert it into a number, which would be 20 and add it to another date field, such as 08/01/2014. So the result would be 08/21/2014.

Thanks!


Solution

  • Try this

    SELECT DATEADD(day, cast(left('20D',2)as integer), '08/01/2014');