Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Substring Grab all but last three characters

Basically, I am trying to find the inverse of this command: substring(term, -3).

like image 631
Spencer Avatar asked Oct 14 '11 16:10

Spencer


People also ask

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 do you order last 3 characters?

SELECT *FROM yourTableName ORDER BY RIGHT(yourColumnName,3) yourSortingOrder; Just replace the 'yourSortingOrder' to ASC or DESC to set the ascending or descending order respectively. Here is the query to order by last 3 chars.

How can I remove last 5 characters from a string in SQL?

Below is the syntax for the SUBSTRING() function to delete the last N characters from the field. 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.

How do you get the first three or last three characters of a name column?

You can use LEN() or LENGTH()(in case of oracle sql) function to get the length of a column. SELECT LEN(column_name) FROM table_name; And you can use SUBSTRING or SUBSTR() function go get first three characters of a column.


2 Answers

Try this:

SELECT SubStr(myColumn, 1, LENGTH(myColumn) - 3)
FROM MyTable

or

SELECT LEFT(myColumn, LENGTH(myColumn) - 3)
FROM MyTable
like image 87
Abe Miessler Avatar answered Sep 29 '22 14:09

Abe Miessler


This should do it: SELECT SUBSTRING(term FROM 1 FOR LENGTH(term)-3)

like image 41
Dvir Avatar answered Sep 29 '22 12:09

Dvir