Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all but the first character in a string

How can you return a string minus the first character in a string in MySQL?

In other words, get 'ello' from 'hello'.

The only way I can think to do it is to use mid() with the second offset being larger than the string could possibly be:

select mid('hello', 2, 99)

But I'm convinced there must be a more elegant way of doing it. Is there?

like image 567
ʞɔıu Avatar asked Dec 05 '22 07:12

ʞɔıu


2 Answers

Use SUBSTR (or SUBSTRING):

SELECT SUBSTR( 'hello', 2 );
--> 'ello'

See also: MySQL String Functions

like image 189
Rob Avatar answered Dec 10 '22 09:12

Rob


That is actually incorrect. If you do that, you only get llo. In order to get everything after the first character....do the following:

<?php
echo substr('hello', 1); // Output ello
echo '<br />';
echo substr('hello', 2); // Output llo
?>

I just wanted to correct that.

Edit: Bah, ignore me. I thought you were talking about in PHP. Well anyway, you can do that in Mysql..or you can just get it in PHP as I posted above.

like image 33
Ninjakreborn Avatar answered Dec 10 '22 11:12

Ninjakreborn