Hi does anyone know how to remove the last three characters from a column with an undifined string length in SQL...
Example...
SELECT * from animals
Would currently return:- elephant,monkey,frog
I would like it to return:- eleph,mon,f
Any help will me much appreciated Ian
Returning the first three characters from a string
Moderator: General Moderators
- icarpenter
- Forum Commoner
- Posts: 84
- Joined: Mon Mar 07, 2005 8:12 am
- Location: Kent, England
try somethign like this:
Code: Select all
<?
$newString = "elephant";
$len = strlen($newString);
$newString = substr($newString,0,$len-3);
echo $newString;
?>Most SQL products have a SUBSTR function (Sometimes the help of STRLEN is required)
Fe MySQL:
Edit: Btw, if LENGTH(column) -3 < 3 you'll have to use 3. But with the IF function you can handle that.
Fe MySQL:
Code: Select all
SELECT SUBSTRING(column, 1, LENGTH(column) -3)- icarpenter
- Forum Commoner
- Posts: 84
- Joined: Mon Mar 07, 2005 8:12 am
- Location: Kent, England