Forum Moderators: coopster

Message Too Old, No Replies

Selecting only rows beginning with a number.

         

dkin

4:52 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



I have a small portion of my site dedicated to sorting objects by letter, but when they start with a number I am stuck. Anyone know how to do this?

vincevincevince

5:05 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you explain a bit more? Are you talking MySQL here?

dkin

5:14 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



sorry, yes I am talking mysql.

rows are like this

Jeffrey
Jason
jackie
7Donna
78 Billy

etc

I only want to select the ones which begin with a number.

jatar_k

5:26 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



here is a wild guess on my part, my regex sucks but i was bored

select * from mytable where mycolumn REGEXP '^[1-9]';

anyone do better than that?

Philosopher

5:29 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like you could use a bit of pattern matching for what you need.

[dev.mysql.com...]

Should give you all the info needed to do exactly what you want (Select only records beginning with number).

Example:

Select * from table_name where column REGEXP '^[0-9]';

Should find all records beginning with a number.

[edit]jatar_k was faster (but I included 0). :)[/edit]

sun818

5:33 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



mySQL reference manual has a lot of examples. You can try the left function. Select the first character between 0 and 9.

[dev.mysql.com...]

LEFT(str,len)

Returns the leftmost len characters from the string str.

mysql> SELECT LEFT('foobarbar', 5);
-> 'fooba'

dkin

5:42 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



works beautifully, thanks jatar and philosopher

jatar_k

6:12 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, oh yeah 0, is that a digit these days ;)

coopster

8:50 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Not always, sometimes it is a slang named applied to people that forget that it is a digit ;)

jatar_k

8:57 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



everybody's a comedian ;)