Page 1 of 1

explode in mysql

Posted: Fri Jan 25, 2008 1:04 am
by thatsme
I have field values in the form,

11-0010
11-2001

How to get the first characters before - character.
at present i am using,

SELECT * FROM table_name WHERE LEFT(field, 2) =$exploded_val[1];
this works fine. But it works only if the number of characters are 2 (that is 11 in the example ). Is there any regular expression function in mysql where i can get all the characters before the (-) character?

Re: explode in mysql

Posted: Fri Jan 25, 2008 1:42 am
by Christopher
Easier might be:

Code: Select all

list($first, $second) = explode('-', $value);

Re: explode in mysql

Posted: Fri Jan 25, 2008 2:03 am
by Kieran Huggins
indeed there is a MySQL regex!

http://dev.mysql.com/doc/refman/5.0/en/regexp.html

exploding sounds a lot easier on the DB engine though, arborint++

Re: explode in mysql

Posted: Fri Jan 25, 2008 12:41 pm
by mwasif
You can use MySQL's function SUBSTRING_INDEX().

Code: Select all

SELECT * FROM table_name WHERE SUBSTRING_INDEX(field,'-',1)=$exploded_val[1];
Kieran Huggins wrote:indeed there is a MySQL regex!
http://dev.mysql.com/doc/refman/5.0/en/regexp.html
It returns 1 if the pattern matches otherwise 0.

Re: explode in mysql

Posted: Sun May 10, 2009 1:18 am
by pekarna