Extract the Final Character from a String in SQL Server

I need to build a SELECT query in SQL Server that adds a new column. This column should show the value “Ok” if the last character in a field called ‘string’ is either A, B, or F, and otherwise it should display “No”. I’ve attempted to use substring functions to isolate the final letter, but my approach isn’t working as expected. Any guidance on how to properly extract the last character would be great.

hey there, i was wonderin, have you tried using substring(your_str, len(your_str), 1)? it might help if u check for spaces too. what do u guys think about handling csae sensitivity in these situations?

hey, have you tried rtrim()? if you remove trailing spaces then substring(my_str, len(my_str),1) works better. might be that extra blanks mess things up, hope that helps!

The RIGHT() function is an efficient approach to extract the final character from a string. Instead of relying on SUBSTRING and LEN, RIGHT(string, 1) directly retrieves the last character, making the query more concise. In my experience, this method reduces potential issues with string length and trailing whitespace. However, it is important to combine it with RTRIM if there is any possibility of trailing spaces, ensuring accuracy when comparing against values like A, B, or F.

hey, have u ever thought of combining a case statement with right(rtrim(string),1) to deal w/ any null or special cases? does this method simplify your logic or introduce new issues?

hey, try using right(rtrim(string),1) instead of substring. i’ve seen it checks out better, even with unexpected traling spaces, works more reliable in my case