1

I have trouble to split string and get only numeric value those are in round bracket. I try substring_index but can't get success.

Column

prd_code

HWC-4054 (100 Pcs available)
HWC-7514 (125 pcs available)
HWC-1516 (total 80 pcs available)
HWC-8008 (80pcs available)

Required output

prd_code

100
125

Thank you.

4 Answers 4

2

Just now I tried to solve your issue please look on it :

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('HWC-4054 (100 Pcs available)','(',-1),' ',1)

This is only code snippet, you need to check its suitable for your project or not.

Sign up to request clarification or add additional context in comments.

1 Comment

I think now you need to write stored procedure to achieve your result.
1

Please check below solutions using mysql function.

select prd_code,digits(SUBSTRING_INDEX( SUBSTRING_INDEX( prd_code , '(', -1 ), ')', 1)) as one from `your table`

use below mysql function :

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);

 IF str IS NULL
 THEN 
   RETURN "";
 END IF;

 SET len = CHAR_LENGTH( str );
 REPEAT
   BEGIN
     SET c = MID( str, i, 1 );
     IF c BETWEEN '0' AND '9' THEN 
       SET ret=CONCAT(ret,c);
     END IF;
     SET i = i + 1;
   END;
 UNTIL i > len END REPEAT;
 RETURN ret;
END |
DELIMITER ;

reference from this

1 Comment

It is mandatory to use digits function ? i just say without function it is not possible ?
0
select SUBSTRING_INDEX(REPLACE(SUBSTRING(SUBSTRING_INDEX(ucase(pcode), '(', 2),LENGTH(SUBSTRING_INDEX(ucase(pcode), '(', 2 -1)) + 1),'(', ' '),'P',1) as prd_code from tbltest;

In this tbltest is table name and pcode is column in that table which contains prd_code value

1 Comment

Thank you ! but i get null fields.
0

You can use PATINDEX

SELECT SUBSTRING(Your_FieldName, PATINDEX('%[0-9]%', Your_FieldName), LEN(Your_FieldName))

2 Comments

Thanks. But i get error of PATINDEX does't exist.
Ok. You can use LOCATE() and REGEXP() instead of PATINDEX

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.