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
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
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 ;
It also work with 2 times SUBSTRING_INDEX
SELECT
SUBSTRING_INDEX (SUBSTRING_INDEX( ':"postalCode";s:4:"3150";', '"',-2) , '"', 1);
I would like to remove the trailing spaces from the expressions in my column and add them to beginning of the expression. So for instance, I currently have the expressions:
Sample_four_space
Sample_two_space
Sample_one_space
I would like to transform this column into:
Sample_four_space
Sample_two_space
Sample_one_space
I have tried this expression:
UPDATE My_Table
SET name = REPLACE(name,'% ',' %')
However, I would like a more robust query that would work for any length of trailing spaces. Can you help me develop a query that will remove all trailing spaces and add them to the beginning of the expression?
If you know all spaces are at the end (as in your example, then you can count them and put them at the beginning:
select concat(space(length(name) - length(replace(name, ' ', ''))),
replace(name, ' ', '')
)
Otherwise the better solution is:
select concat(space( length(name) - length(trim(trailing ' ' from name)) ),
trim(trailing ' ' from name)
)
or:
select concat(space( length(name) - length(rtrim(name)) ),
rtrim(name)
)
Both these cases count the number of spaces (in or at the end of). The space() function then replicates the spaces and concat() puts them at the beginning.
I have a column which returns
a:2:{i:0;s:10:"Properties";i:1;s:14:"Movable Assets";}
I would like to return only:
Properties, Movable Assets
How can I use a select statement to retrieve the values between the " symbols
these are serialize values, you can use php, to get your desired results.
you can use , unserialize which will return an array then you can use implode to get the comma separated values.
example
Use SUBSTRING_INDEX().
SUBSTRING_INDEX() takes a string argument followed by a delimiter character and the number of parts to return. After you break up the string using the delimiter, that number of parts is returned as a single string.
select concat(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
'a:2:{i:0;s:10:"Properties";i:1;s:14:"Movable Assets";}',
'"',
4
),
'"',
2
),
'"',
-1
),
",",
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
'a:2:{i:0;s:10:"Properties";i:1;s:14:"Movable Assets";}',
'"',
4
),
'"',
4
),
'"',
-1
)
);
Use combination of LOCATE() and SUBSTRING().
Definitions: https://dev.mysql.com/doc/refman/5.0/en/string-functions.html
Or better -- migrate the data to actually be retrievable.
I am trying to do some string manipulated within a MySQL select and I seem to have some strange behavior from the UPPER function.
I am trying to return the first letter of a word (delimited with spaces) and convert it to upper case. However if I use UPPER on the single returned letter I get a blank, while if I use UPPER on the whole word before getting the first letter from it I do get the first letter.
Stripping the SQL down to the minimum I have come up with this test SQL:-
SELECT
'verbatim h',
SUBSTRING(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(' ','verbatim h',' '), ' ', 2), ' ', -1), 1, 1),
UPPER(SUBSTRING(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(' ','verbatim h',' '), ' ', 2), ' ', -1), 1, 1)),
SUBSTRING(UPPER(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(' ','verbatim h',' '), ' ', 2), ' ', -1)), 1, 1)
This is taking the string 'verbatim h', concatenating spaces at either end then getting the string between the 1st and 2nd space (so it will get verbatim).
The first column is the full string, the 2nd column is the first letter of the first word, the 3rd column is the first letter converted to upper case of the first word while the 4th column is the first letter of the first word converted to upper case.
I think columns 3 and 4 should have the same values (the only difference being that one converts the 1st word to upper case before grabbing the first letter, while the other grabs the 1st letter then converts it to upper case), but instead one contains the letter V as I would expect while the other contains blank.
If I modify the above to get the HEX values of the resulting characters, the blank one is a 1 character string with hex value 00, while the V is a hex value of 56.
Any suggestions? Am I missing something obvious?
The string becomes a binary string. And for these you can't use LOWER and UPPER, as stated in the mysql reference docu
So how to solve?
Use the convert funcion like this:
SELECT
'verbatim h',
SUBSTRING(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(' ','verbatim h',' '), ' ', 2), ' ', -1), 1, 1) AS c1,
UPPER(CONVERT((SUBSTRING(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(' ','verbatim h',' '), ' ', 2), ' ', -1), 1, 1)) USING latin1)) AS c1_upper;
here is a sqlfiddle
I am trying to extract a certain part of a column that is between delimiters.
e.g. find foo in the following
test 'esf :foo: bar
So in the above I'd want to return foo, but all the regexp functions only return true|false,
is there a way to do this in MySQL
Here ya go, bud:
SELECT
SUBSTR(column,
LOCATE(':',column)+1,
(CHAR_LENGTH(column) - LOCATE(':',REVERSE(column)) - LOCATE(':',column)))
FROM table
Yea, no clue why you're doing this, but this will do the trick.
By performing a LOCATE, we can find the first ':'. To find the last ':', there's no reverse LOCATE, so we have to do it manually by performing a LOCATE(':', REVERSE(column)).
With the index of the first ':', the number of chars from the last ':' to the end of the string, and the CHAR_LENGTH (don't use LENGTH() for this), we can use a little math to discover the length of the string between the two instances of ':'.
This way we can peform a SUBSTR and dynamically pluck out the characters between the two ':'.
Again, it's gross, but to each his own.
This should work if the two delimiters only appear twice in your column. I am doing something similar...
substring_index(substring_index(column,':',-2),':',1)
A combination of LOCATE and MID would probably do the trick.
If the value "test 'esf :foo: bar" was in the field fooField:
MID( fooField, LOCATE('foo', fooField), 3);
I don't know if you have this kind of authority, but if you have to do queries like this it might be time to renormalize your tables, and have these values in a lookup table.
With only one set of delimeters, the following should work:
SUBSTR(
SUBSTR(fooField,LOCATE(':',fooField)+1),
1,
LOCATE(':',SUBSTR(fooField,LOCATE(':',fooField)+1))-1
)
mid(col,
locate('?m=',col) + char_length('?m='),
locate('&o=',col) - locate('?m=',col) - char_length('?m=')
)
A bit compact form by replacing char_length(.) with the number 3
mid(col, locate('?m=',col) + 3, locate('&o=',col) - locate('?m=',col) - 3)
the patterns I have used are '?m=' and '&o'.
select mid(col from locate(':',col) + 1 for
locate(':',col,locate(':',col)+1)-locate(':',col) - 1 )
from table where col rlike ':.*:';
If you know the position you want to extract from as opposed to what the data itself is:
$colNumber = 2; //2nd position
$sql = "REPLACE(SUBSTRING(SUBSTRING_INDEX(fooField, ':', $colNumber),
LENGTH(SUBSTRING_INDEX(fooField,
':',
$colNumber - 1)) + 1)";
This is what I am extracting from (mainly colon ':' as delimiter but some exceptions), as column theline255 in table loaddata255:
23856.409:0023:trace:message:SPY_EnterMessage (0x2003a) L"{#32769}" [0081] WM_NCCREATE sent from self wp=00000000 lp=0023f0b0
This is the MySql code (It quickly did what I want, and is straight forward):
select
time('2000-01-01 00:00:00' + interval substring_index(theline255, '.', 1) second) as hhmmss
, substring_index(substring_index(theline255, ':', 1), '.', -1) as logMilli
, substring_index(substring_index(theline255, ':', 2), ':', -1) as logTid
, substring_index(substring_index(theline255, ':', 3), ':', -1) as logType
, substring_index(substring_index(theline255, ':', 4), ':', -1) as logArea
, substring_index(substring_index(theline255, ' ', 1), ':', -1) as logFunction
, substring(theline255, length(substring_index(theline255, ' ', 1)) + 2) as logText
from loaddata255
and this is the result:
# LogTime, LogTimeMilli, LogTid, LogType, LogArea, LogFunction, LogText
'06:37:36', '409', '0023', 'trace', 'message', 'SPY_EnterMessage', '(0x2003a) L\"{#32769}\" [0081] WM_NCCREATE sent from self wp=00000000 lp=0023f0b0'
This one looks elegant to me. Strip all after n-th separator, rotate string, strip everything after 1. separator, rotate back.
select
reverse(
substring_index(
reverse(substring_index(str,separator,substrindex)),
separator,
1)
);
For example:
select
reverse(
substring_index(
reverse(substring_index('www.mysql.com','.',2)),
'.',
1
)
);
you can use the substring / locate function in 1 command
here is a mice tutorial:
http://infofreund.de/mysql-select-substring-2-different-delimiters/
The command as describes their should look for u:
**SELECT substr(text,Locate(' :', text )+2,Locate(': ', text )-(Locate(' :', text )+2)) FROM testtable**
where text is the textfield which contains "test 'esf :foo: bar"
So foo can be fooooo or fo - the length doesnt matter :).