MySQL Forums
Forum List  »  General

Re: Convert string into a number
Posted by: Rick James
Date: October 18, 2010 07:16PM

If it is a constant length, then
SET @h = HEX('jkl');
SELECT MID(@h, 1, 2),
       MID(@h, 3, 2),
       MID(@h, 5, 2);
+---------------+---------------+---------------+
| MID(@h, 1, 2) | MID(@h, 3, 2) | MID(@h, 5, 2) |
+---------------+---------------+---------------+
| 6A            | 6B            | 6C            |
+---------------+---------------+---------------+
SELECT LEFT(@h, 1),
       LEFT(@h, 2),
       LEFT(@h, 3),
       LEFT(@h, 4),
       LEFT(@h, 5),
       LEFT(@h, 6);
+-------------+-------------+-------------+-------------+-------------+-------------+
| LEFT(@h, 1) | LEFT(@h, 2) | LEFT(@h, 3) | LEFT(@h, 4) | LEFT(@h, 5) | LEFT(@h, 6) |
+-------------+-------------+-------------+-------------+-------------+-------------+
| 6           | 6A          | 6A6         | 6A6B        | 6A6B6       | 6A6B6C    |
+-------------+-------------+-------------+-------------+-------------+-------------+
If it is variable length, then consider a stored procedure, where you can loop.

Better yet, do it in Perl or PHP.

Options: ReplyQuote


Subject
Written By
Posted
October 17, 2010 03:36PM
October 17, 2010 11:18PM
Re: Convert string into a number
October 18, 2010 07:16PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.