I have a column that consists of details of an orderline named 'ConcatValue'. An example of a value in this column is:
573856014/100/M00558640/OrderQty12
I want to extract the order value which can be founded after 'OrderQty'. I thought I had a solution by executing the following statement: substr(ConcatValue,char_length(ConcatValue)-1,char_length(ConcatValue))
This results in only level the last 2 characters of the string from the column ConcatValue. For the ConcatValue mentioned above I will get the following result: '12'. Which is the desired result.
But when the orderline has an Order quantity below 10, for example in the following ConcatValue:573856014/100/M00558640/OrderQty3
I will get the following result: y3
My question: Is there a way to delete 'y' if a row has an y within the value? Or is there a way to replace the y with a 0? Or is there a way to only select the last digits from the ConcatValue string?
Use string functions.
With substring_index() you can get the last part of the string and with replace() remove 'OrderQty':
select replace(
substring_index(ConcatValue, '/', -1),
'OrderQty',
''
)
from tablename
Actually, the simplest method is simply:
select substring_index(ConcatValue, 'OrderQty', -1)
Related
My value is "asdsdf2739173sidfsd"
Here I want to get first and last occurrence of any number position or index in given string, please help me
To find the indices of the first and last occurrence of a number in a string, we can try using REGEXP_REPLACE:
WITH yourTable AS (
SELECT 'asdsdf2739173sidfsd' AS val
)
SELECT
val,
LENGTH(val) - LENGTH(REGEXP_REPLACE(val, '^[^0-9]*', '')) AS idx_first,
LENGTH(REGEXP_REPLACE(val, '[^0-9]*$', '')) - 1 AS idx_last
FROM yourTable;
Demo
I have the following Varchar data in a column in my MYSQL table:
Blank_Person_ID_776
Person_999
I want to extract the final number after the underscore to a variable (in this case 776) in order to use it in a query. I.e. ignore any underscore but the last one.
How can I do so?
I would like my final query to be as follows:
SET #personId= //query to get id;
Update Person set tracking_id = #personId where tracking_id is null;
If you want the final value after the last '_', use substring_index():
select substring_index(<whatever>, '_', -1)
If you specifically want the final number in the string, even when there are characters after:
select regexp_replace(<whatever>, '.*_([0-9]+)[^0-9]*$', '$1')
I'd like to extract the number between NUMBER and ;. So far I can extract the data up to the number, but I don't want anything after the number. e.g.,
SELECT
SUBSTRING(field, LOCATE('NUMBER=', rrule) + 7)
FROM table
Data field:
DATA:PASS=X12;NUMBER=331;FIELD=1
DATA:PASS=X12;NUMBER=2;FOO=BAR;FIELD=1
Desired Output:
331
2
You can use a combination of SUBSTRING_INDEX functions:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(field, 'NUMBER=', -1),
';',
1)
FROM
tablename
Please see an example fiddle here.
The inner SUBSTRING_INDEX will return everything after the NUMBER= string, while the second will return everything before the ; returned by the inner function.
It is not my code, its something that I need to get it done without modifying the structure of table. I know it would be very easy to just store date as MySQL date format but I cant do that.
There is a column in table which stores serialized array as a string. Now I need to select all rows whose 'date' is less than today.
This date is inside serialized array string.
Is there a way to compare it on mysql query? An example string is:
a:3:{s:4:"test";b:1;s:2:"se";i:1;s:4:"date";s:10:"2013-05-23";}
I need to compare the "date" from this string to mysql date using the following query:
"date" BETWEEN 2013-01-01 AND 2013-05-23
You can extract the date value (assuming it's always set off by "date";s:10) using nested SUBSTRING_INDEX calls. The inner one returns everything after "date";s:10" and the outer one cuts off the closing quote and whatever follows:
SUBSTRING_INDEX(SUBSTRING_INDEX(val, '"date";s:10:"', -1), '"', 1)
If val is a:3:{s:4:"test";b:1;s:2:"se";i:1;s:4:"date";s:10:"2013-05-23";} as in your example, this will return 2013-05-23. Then your query can be:
...
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(val, '"date";s:10:"', -1), '"', 1) BETWEEN 2013-01-01 AND 2013-05-23
Not pretty, but we can't expect pretty here :)
I think you should get date with substring with starting character -13 (13 from right side) and length of 10.
Something like this:
SUBSTR(field_name, -13, 10)
select * from postmeta where meta_key = 'your_meta_key' and meta_value REGEXP ('6')
I have a table like:
id name
--------
1 clark_009
2 clark_012
3 johny_002
4 johny_010
I need to get results in this order:
johny_002
clark_009
johny_010
clark_012
Do not ask me what I already tried, I have no idea how to do this.
This will do it, very simply selecting the right-most 3 characters and ordering by that value ascending.
SELECT *
FROM table_name
ORDER BY RIGHT(name, 3) ASC;
It should be added that as your data grows, this will become an inefficient solution. Eventually, you'll probably want to store the numeric appendix in a separate, indexed integer column, so that sorting will be optimally efficient.
you should try this.
SELECT * FROM Table order by SUBSTRING(name, -3);
good luck!
You may apply substring_index function to parse these values -
select * from table order by substring_index(name, '_', -1)
You can use MySQL SUBSTRING() function to sort by substring
Syntax : SUBSTRING(string,position,length)
Example : Sort by last 3 characters of a String
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, -3);
#OR
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, -3,3);
Example : Sort by first 3 characters of a String
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, 1,3);
Note : Positive Position/Index start from Left to Right and Negative Position/Index start from Right to Left of the String.
Here is the details about SUBSTRING() function.
If you want to order by the last three characters (from left to right) with variable name lengths, I propose this:
SELECT *
FROM TABLE
ORDER BY SUBSTRING (name, LEN(name)-2, 3)
The index starts at lenght of name -2 which is the third last character.
I'm a little late but just encountered the same problem and this helped me.