0

I would need to get value from given regexp.

For example:

> :"postalCode";s:4:"3150";

Is there any way I can extract 3150, from this part of column value. Column value stored serialized objects, so postalCode variable can be null type, that way I should check if positive integer follows ;s:POSITIVE_INT:"postalCodeValue

2
  • What sort of data is this? I was almost tempted to say JSON, but not quite. Commented Dec 26, 2017 at 14:11
  • it's PHP serialized data @TimBiegeleisen php.net/manual/en/function.serialize.php Commented Dec 26, 2017 at 15:07

3 Answers 3

2

Use SUBSTRING_INDEX:

SELECT
    SUBSTRING(SUBSTRING_INDEX(col, '"', -2), 1,
              INSTR(SUBSTRING_INDEX(col, '"', -2), '"') - 1) AS num
FROM yourTable;

This query will extract the last quoted number in your string.

Demo

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

3 Comments

I am sorry, I forgot to mention that this is only part of huge value that is stored in column type so it is in the middle of value this part. It has some random value before and after it (:"postalCode";s:4:"3150"; is not full string on which I call these procedures), and I should point on it and fetch value.
Does s:4: always separate the postal code from the number?
Not, 4 is length of postalCode which is after that inside quotes. It can have different values. But ";s:11:"countryName always comes after postalCode value which I need.
0

avoiding regexp you could use some string function eg:

SELECT LENGTH(':"postalCode";s:4:"3150"') - LOCATE(':', REVERSE(':"postalCode";s:4:"3150"'))+1
from dual ;

or

SELECT LENGTH(col_name) - LOCATE(':', REVERSE(col_name))+1
from my_table ;

Comments

0

It also work with 2 times SUBSTRING_INDEX

SELECT 
SUBSTRING_INDEX (SUBSTRING_INDEX( ':"postalCode";s:4:"3150";', '"',-2) , '"', 1);

Comments

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.