Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discard last 3 characters of a field

Tags:

string

mysql

I want to extract the first (not last three) characters of a string from a table like:

Code
--------
CREF182
CXEF7U8
CEF7U8
CF777

How can I perform a query that will discard the last three characters of the string in a result so it shows something like:

Code
--------
CREF
CXEF
CEF
CF

I'm thinking this should be the Opposite of mysql RIGHT function? Any suggestions?

like image 925
rtuner Avatar asked Jan 29 '26 20:01

rtuner


2 Answers

Try this one...

Syntax

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

Example

SELECT SubStr(code, 1, LENGTH(code) - 3)
    FROM MyTable
like image 153
Ganesh Rengarajan Avatar answered Jan 31 '26 09:01

Ganesh Rengarajan


How about:

SELECT REVERSE(SUBSTR(REVERSE(Code), 4)) as Code from YourTable;
like image 32
Chibueze Opata Avatar answered Jan 31 '26 09:01

Chibueze Opata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!