Changing the format of the entire column in a mysql table - mysql

I have a column in a mysql table like this:
Testtag
SG_QRA_SGBAKE_0012
SG_QRA_SGBAKE_0013
SG_QRA_SGHAST_0005
SG_QRA_SGHAST_0006
SG_QRA_SGHAST_0007
and I want to change the entire column to like this:
Testtag
SGBAKE.0012
SGBAKE.0013
SGHAST.0005
SGHAST.0006
SGHAST.0007
I am stuck here. Any help is appreciated thanks!

We can handle your requirement using SUBSTRING_INDEX along with REPLACE:
UPDATE yourTable
SET Testtag = REPLACE(SUBSTRING_INDEX(Testtag, '_', -2), '_', '.');
If you just want to view your data this way, then use this query:
SELECT Testtag, REPLACE(SUBSTRING_INDEX(Testtag, '_', -2), '_', '.') AS NewTag
FROM yourTable;
Here is a working demo.

Related

Split string in MYSQL via CRATE

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

MYSQL Sorting Using Order by

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

Getting the name with dashes using substring_index MySQL

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;

mysql pick particular string only

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

I would like to replace the text in a column from "300-21-2" to "300-21-02" with one query

Is there an easy way to replace all the text in a VARCHAR 255 column from "300-21-2" to "300-21-02" with one query?
Thank you.
This is basic SQL
UPDATE tablename
SET columnname = '300-21-02'
WHERE columnname = '300-21-2'
If the pattern is always the same NNN-NN-N then what you need is:
update tablex
set column = concat( substr(column,1,7), lpad(substr(column,8),2,'0') )
see it at fiddle:
http://sqlfiddle.com/#!2/f59fe/1
EDIT As the op showed the pattern
update tablex
set column = CONCAT(
substring_index(col, '-',1), '-',
lpad(substring_index(substring_index(col, '-',-2), '-', 1),2,'0'), '-',
lpad(substring_index(col, '-',-1), 2, '0') )
If you like to convert the first set like 300 to 00300 as your pattern you add the lpad as this: lpad(substring_index(col, '-',1),5,'0')
This should be a lot easier if mysql has support to regex replace, but as it hasnt you have to work with the strings:
from this value: '300-02-1'
from substring_index(col, '-',1) I'm getting: 300
from substring_index(substring_index(col, '-',-2), '-', 1) I'm getting 02 I did this because just put the substring_index(col, '-',2) gave me 300-02 so, i got it from right to left (-2) then i get the first
and substring_index(col, '-',-1) it bring me 1 because it gets the value from right to left
Then I just concatenate it all formatting the ones I want.