Hi I need help in sorting mysql query.
I have a column, where it contains the data like
R_5_TP(6),
R_5_TP(7),
R_5_TP(8),
R_6_TP(1),
R_6_TP(10),
R_6_TP(6),
R_50_TP(1),
R_7_TP(1),
I need to write a select query using order by and get the result as
R_5_TP(6),
R_5_TP(7),
R_5_TP(8),
R_6_TP(1),
R_6_TP(6),
R_6_TP(10),
R_7_TP(1),
R_50_TP(1),
Please help.Thank you.
This is a bit tricky. I think you want:
order by substring_index(substring_index(col, '_', 2), '_', -1) + 0, -- convert middle value to a number
substring_index(col, '(', -1) + 0 -- convert parenthesized value to number
Related
Problem is to write sql script to delete leading zeros i MySQL database table in one column which represents identifires (numbers and dots).
I want to update values in db from example "1.2.03.4" to "1.2.3.4"
You may try replace for same. It seems like IP address, so you may check any 0 after . need to be removed i,e: .0 replaces with '.' .
select replace ( #str, '.0','.')
MySQL 8+ has regexp_replace() which does exactly what you want:
select regexp_replace('1.2.03.00004', '[.]0+', '.')
EDIT:
If you want to replace only the leading zeros on the third value, then you can use substring_index():
select concat_ws('.',
substring_index(col, '.', 2),
substring_index( substring_index(col, '.', 3), '.', -1 ) + 0, -- convert to number
substring_index(col, '.', -1)
)
I want to split a string in CRATE. I tried with substr, but it takes only substr(string,long,long). I want something like a function which can take delimiter string.
Example :
value=1234-5656
select SUBSTR(value, '-',1) as first from XYZ;
I want to split the value into 1234 and 5656 in a SQL query. But CRATE does not support SUBSTR(value, '-',1). So I am looking for an option to split the value in the CRATE query.
Any help?
SUBSTRING_INDEX comes in handy here:
SELECT
SUBSTRING_INDEX('1234-5656', '-', 1) AS first,
SUBSTRING_INDEX('1234-5656', '-', -1) AS second
FROM yourTable;
please try using this query:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(name, '_', 1), '_', -1) as beforesplit, SUBSTRING_INDEX(SUBSTRING_INDEX(name, '_', 2), '_', -1) as aftersplit FROM testenter
For CrateDB you probably want to use regex_matches function more info on Create's documentation site
However the following should give you what you're looking for
select regexp_matches(yourColumnName, '([0-9])\w+')[1] from yourTable
I am using MySql and I have a table with the column in the form of
first/first1/first2/first3
and
second/second1/second2/second3
I want to perform ordering on the column ignoring the first value of every column before '/'.
i.e. perform ordering on
first1/first2/first3
and
second1/second2/second3
In simple words, I want to perform orderby from first value after '/'.
Any help will be appreciated. Thanks!
Try this one as a sorting criterion:
order by substr(col, instr(col, '/') + 1)
Explanation: The expression extracts the potion after the first occurrence of /. A refined version would cater for values without the separator char, eg.:
order by substr(col, coalesce(instr(col, '/'), 0) + 1)
SELECT .....
FROM ....
ORDER BY SUBSTRING(column FROM INSTR(column, '/') + 1)
I have a data that have this result:
I'm using the query
select substring_index(descriptn, ' ', -1) from table1
and I get my result along with the dash for example "JACQ-ARMIE"
I want to get only JACQ and ABBY. Can you give me hints on how to get the name? Does this also apply for substring_index?
Just use substring_index() again:
select substring_index(substring_index(descriptn, ' ', -1), '-', 1)
from table1;
I have a column in database and having value like this
course_repeatfkfjkjfjkfer_10_topics_0_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_1_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_2_presentation_link
coursek_epeatfkfjfkfkfklfflkflkfs_10_presentation_link
course_hdhdhhdhdjhdrepeatfkfjfkfkfklfflkflkfs_21_presentation_link
and so on.
I need to pick 0,1,2,10,21, number before _presentation_link , But i need this in mysql as well
i used substr in mysql, but that is not working. Any idea?
Thanks
One option would be to use a combination of SUBSTRING_INDEX() and REPLACE():
SELECT SUBSTRING_INDEX(REPLACE(col, '_presentation_link', ''), '_', -1)
FROM yourTable
Taking course_repeatfkfjkjfjkfer_10_topics_0_presentation_link as an example, after the replacement, this would become:
course_repeatfkfjkjfjkfer_10_topics_0
The call to SUBSTRING_INDEX() then grabs everything appearing after the final underscore, which is the number you want to capture.
Demo here:
SQLFiddle
You can use substring_index twice like this:
select substring_index(substring_index(col, '_', -3), '_', 1)
from t
Demo