3

I'm stuck with a problem, in my database i've values like this:

GSB45-B, GSBD60-01, etc.

Now i need to get the values GSB / GSBD out of these strings. So in fact i need te first character from te start to the first integer.

Hopefully someone can help me, thanks from now.

3
  • So you want 45-B and 60-01 ? Commented Jun 21, 2012 at 8:42
  • No, i need GSB, GSBD. (the chars till the first integrer). Thanks. Commented Jun 21, 2012 at 8:45
  • @Ronn0 : Could you please check my answer and let me know if that is fine with you? Commented Jun 22, 2012 at 12:02

4 Answers 4

2

It's a bit of a clunky solution, but you can try:

SELECT 
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(SUBSTRING_INDEX(column, '-', 1), '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', '') AS extracted
FROM
    yourtbl
Sign up to request clarification or add additional context in comments.

1 Comment

Great! I'm glad that it works and you only need to run it once! I'm sure many others will laugh hysterically at first sight of all those REPLACEs, but it's really the only way you can do it AFAIK! =)
1
DELIMITER |
CREATE FUNCTION digits( str CHAR(32) ) RETURNS CHAR(32)
BEGIN
  DECLARE i, len SMALLINT DEFAULT 1;
  DECLARE ret CHAR(32) DEFAULT '';
  DECLARE c CHAR(1);
  SET len = CHAR_LENGTH( str );
  label1: LOOP
  REPEAT
    BEGIN
      SET c = MID( str, i, 1 );
      IF c BETWEEN '0' AND '9' THEN 
        SET ret=SUBSTRING( str, 1, i-1);

        LEAVE label1;
      END IF;
      SET i = i + 1;
    END;
  UNTIL i > len END REPEAT;
  END LOOP label1;
  RETURN ret;
END |
DELIMITER ;

select digits('GSB45-B'),digits('GSBD60-01') ,digits('KKGSBD60-01') ;

Comments

1

How about this??

SELECT 
  myWord, 
  SUBSTR(myWord,1,LEAST (
    if (Locate('0',myWord) >0,Locate('0',myWord),999),
    if (Locate('1',myWord) >0,Locate('1',myWord),999),
    if (Locate('2',myWord) >0,Locate('2',myWord),999),
    if (Locate('3',myWord) >0,Locate('3',myWord),999),
    if (Locate('4',myWord) >0,Locate('4',myWord),999),
    if (Locate('5',myWord) >0,Locate('5',myWord),999),
    if (Locate('6',myWord) >0,Locate('6',myWord),999),
    if (Locate('7',myWord) >0,Locate('7',myWord),999),
    if (Locate('8',myWord) >0,Locate('8',myWord),999),
    if (Locate('9',myWord) >0,Locate('9',myWord),999)
  )-1) as NewString
FROM myTable;

Demo

Also read, Using alias name in another column. This is same question as yours.

4 Comments

Unfortunately, REGEXP only returns 0 or 1 based on if it matched the pattern or not, not the extracted match. As a result, your LOCATE() will only be able to return 0 or -1, not the index of the first number.
No, because the value passed to LOCATE() will only be a boolean 0 or 1, not the index of the first occurrence of a number as you would intuitively expect. It's a shame that MySQL doesn't support extracting strings based on patterns...
They both give:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REGEXP '[0-9]')-1) AS 'the_string' FROM products LIMIT 0, 30' at line 1.
@FahimParkar SQL Fiddle is back now, btw. Sorry about the outage!
0

MySQL can't do REGEXP extraction, and that's a really sad thing :(

Assuming that the pattern of your values is [A-Z]*[0-9]{2}-.* you can use this ugly thing:

SELECT
    LEFT(
        SUBSTRING_INDEX("GSB45-B", "-", 1),
        LENGTH(SUBSTRING_INDEX("GSB45-B", "-", 1)) - 2
    ) as CODE;

But I would recommand doing your parsing on the application side :)

3 Comments

what if string is GSB4543-B? Isn't your code will change to - 4
This almost worked, but i did post a bad example. There are also codes with 3 digits GSBD603-03. Thanks for the help!
Indeed Fahim Parkar, that's why I made the assumption on the pattern :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.