sql-servert-sql

How to get last character of a string in sql server


I want a SELECT statement that creates a new column where if a field called "string" ends with the letter A, B, or F it gets the value "Ok" otherwise it gets the value "No".

I know I need to do some substring but cannot get it right.


Solution

  • Use RIGHT:

    SELECT Result = CASE WHEN RIGHT(string, 1) IN ('A','B','F') 
                    THEN 'Ok' ELSE 'No' END