Since your conditions are pretty specific you can do this:
set @a='RA100';
select IF(@a REGEXP '[A-Za-z]{2}', SUBSTRING(@a,1,2), SUBSTRING(@a,1,1)) as
letter, IF(@a REGEXP '[A-Za-z]{2}', SUBSTRING(@a,3), SUBSTRING(@a,2));
+--------+--------+
| letter | number |
+--------+--------+
| RA | 100 |
+--------+--------+
If this looks ugly, then that should be good motivation not to store multiple
pieces of data as concatenated strings =)
Regards,
Gavin Towey
-----Original Message-----
From: Adam Gray [mailto:[email protected]]
Sent: Tuesday, August 03, 2010 8:04 AM
To: [email protected]
Subject: Split string by regex
Hello,
I'm working on a library OPAC system with books classified using the Library of
Congress classification system. This takes the format of either one or two
letters followed by some numbers, i.e. R272 or RA440 etc. What I want to do is
split the field that holds this classification into two, one containing the
letter portion and the other containing the number bit. So
+-------+
| Class |
+-------+
| R100 |
+-------+
| RA65 |
+-------+
| RP2 |
+-------+
Would become
+--------+--------+
| Class | Class2 |
+--------+--------+
| R | 100 |
+--------+--------+
| RA | 65 |
+--------+--------+
| RP | 2 |
+--------+--------+
etc
Could this be done in MySQL? I want to do something along the lines of set
class2 = SUBSTRING_INDEX(class,'[A-Z]',-1) but I understand this is not
possible.
Any ideas?
Regards
Adam
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/[email protected]
This message contains confidential information and is intended only for the
individual named. If you are not the named addressee, you are notified that
reviewing, disseminating, disclosing, copying or distributing this e-mail is
strictly prohibited. Please notify the sender immediately by e-mail if you
have received this e-mail by mistake and delete this e-mail from your system.
E-mail transmission cannot be guaranteed to be secure or error-free as
information could be intercepted, corrupted, lost, destroyed, arrive late or
incomplete, or contain viruses. The sender therefore does not accept liability
for any loss or damage caused by viruses or errors or omissions in the contents
of this message, which arise as a result of e-mail transmission. [FriendFinder
Networks, Inc., 220 Humbolt court, Sunnyvale, CA 94089, USA, FriendFinder.com
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/[email protected]