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
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.
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.
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.)
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.
Use RIGHT
:
SELECT Result = CASE WHEN RIGHT(string, 1) IN ('A','B','F')
THEN 'Ok' ELSE 'No' END
This could to the trick
select right('abc', 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With