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.
Syntax: SELECT SUBSTRING(column_name,1,length(column_name)-N) FROM table_name; Example: Delete the last 2 characters from the FIRSTNAME column from the geeksforgeeks table.
You can use the modulo operator to easily extract the last 6 digits assuming num is a numeric datatype: Show activity on this post.
Right should do:
select RIGHT('abcdeffff',4)
SUBSTR(column, LENGTH(column) - 3, 4)
LENGTH
returns length of string and SUBSTR
returns 4 characters from "the position length - 4"
RIGHT ( character_expression , integer_expression )
SELECT RIGHT(column, 4) FROM ...
Also a list of other string functions.
Use the RIGHT()
function: http://msdn.microsoft.com/en-us/library/ms177532(v=sql.105).aspx
SELECT RIGHT( '1234567890', 4 ); -- returns '7890'
For Oracle SQL, SUBSTR(column_name, -# of characters requested)
will extract last three characters for a given query. e.g.
SELECT SUBSTR(description,-3) FROM student.course;
-
WHERE SUBSTR('Hello world', -4)
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