Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last char in a string in sql server

bit rusty on sql. In a select statement if a field "string" ends with letter A,B or F Print 0k else print No

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

Thanks

like image 507
user9969 Avatar asked Feb 19 '14 09:02

user9969


People also ask

How do I get the last character of a string in SQL?

To get the first n characters of string with MySQL, use LEFT(). To get the last n char of string, the RIGHT() method is used in MySQL.

How do I get the last 3 characters of a string in SQL?

It could be this using the SUBSTR function in MySQL: SELECT `name` FROM `students` WHERE `marks` > 75 ORDER BY SUBSTR(`name`, -3), ID ASC; SUBSTR(name, -3) will select the last three characters in the name column of the student table.

How can I get the last index of a character in SQL Server?

It should be: select left(db_name(), len(db_name()) - charindex('_', reverse(db_name()) + '_') + 1) (I tried to edit, but the change had to be at least 6 characters.)

How do I get the last element in SQL?

We can use the ORDER BY statement and LIMT clause to extract the last data. The basic idea is to sort the sort the table in descending order and then we will limit the number of rows to 1. In this way, we will get the output as the last row of the table.


2 Answers

Use RIGHT:

SELECT Result = CASE WHEN RIGHT(string, 1) IN ('A','B','F') 
                THEN 'Ok' ELSE 'No' END
like image 61
Tim Schmelter Avatar answered Sep 20 '22 09:09

Tim Schmelter


This could to the trick

select right('abc', 1)
like image 41
Andrew Avatar answered Sep 21 '22 09:09

Andrew