I have a table where I extract some values, one column values can contain
'FLSD202-D-B-D-AB-C1-NN-A-N-LA-J-NN/UM/H7/SCT'
I want to split them become
FLSD202 AS first column
-D-B-D-AB-C1-NN-A-N-LA-J-NN AS Second column
/UM/H7/SCT AS third column
here my script but i can't get the result as i want
SELECT 'FLSD202-D-B-D-AB-C1-NN-A-N-LA-J-NN/UM/H7/SCT', SUBSTRING_INDEX(SUBSTRING_INDEX('FLXA202-D-B-D-AB-C1-NN-A-N-LA-J-NN/UM/H7/SCT', '-',1), '-', -1) FirstColumn,
CONCAT('-', SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX('FLSD202-D-B-D-AB-C1-NN-A-N-LA-J-NN/UM/H7/SCT', '/',1), '/', -1), '-',-1)) SecondColumn,
CONCAT('/', SUBSTRING_INDEX(SUBSTRING_INDEX('FLSD202-D-B-D-AB-C1-NN-A-N-LA-J-NN/UM/H7/SCT', '/',3), '/', -1)) ThirdColumn
I want to use this for ALL values, not just one field..
And the '-'(dash) is dynamic and '/'(slash) is dynamic, ex :
SSS145-SS3/POP, KEEE-CDE0/NO/SA, WXAE-C-D/E/G, SAD-SEU-SFX/OPS
Thanks
I think below code will help you to complete your requirement.
SET #a = 'FLSD202-D-B-D-AB-C1-NN-A-N-LA-J-NN/UM/H7/SCT';
SET #dash_start_position = LOCATE('-',#a);
SET #slash_start_position = LOCATE('/',#a);
SELECT SUBSTRING(#a, 1, #dash_start_position-1) as First, IF(#slash_start_position IS NOT NULL AND #slash_start_position > 0, SUBSTRING(#a, #dash_start_position, #slash_start_position - #dash_start_position), SUBSTRING(#a, #dash_start_position)) as Second,SUBSTRING(#a, #slash_start_position) as Third;
Related
I have this code:
SET #startEndTimeCapacity = "'00:00:00', '08:00:00', 120";
SELECT SUBSTRING_INDEX(#startEndTimeCapacity, ",", 2);
results in '00:00:00', '08:00:00'.
How can I get the 2nd value '08:00:00' only?
You can apply the SUBSTRING_INDEX function twice:
for splitting at the nth value
for retrieving the last value in the previous list
Here's how:
SUBSTRING_INDEX(SUBSTRING_INDEX(#startEndTimeCapacity, ",", 2), ',', -1);
This works for any n. In this specific case n=2.
I have a field with value like this 'DOOR-LEFT' and I want to change this to 'Door-LEFT'.
I came across this query on this site:
UPDATE tbl
SET field1 = CONCAT(UCASE(LEFT(field1, 1)),
LCASE(SUBSTRING(field1, 2)));
The above query changes 'DOOR-LEFT' to 'Door-left'. I do not want anything after the - to be updated. So it should be 'Door-LEFT'.
How can I do this?
You can use sustring_index() to split the string on '-', and then use the logic you already have at hand:
update tbl
set field = concat(
upper(left(substring_index(field1, '-', 1), 1)),
lower(substr(substring_index(field1, '-', 1), 2)),
'-',
upper(substring_index(field1, '-', -1))
)
The surrounding upper() in the last part of the string is not strictly needed for your sample string, which is all upper case to start with. I left it on purpose in case it might be useful for other cases (and it doesn't hurt anyway).
I have a table with some values like below,
Slno
---------
IFAAA1121
IFAAA1122
IMBBB1121
IMBBB11223
My goal is to reformat the SlNo in to the below format,
Slno
---------
IF-AAA-1121
IF-AAA-1122
IM-BBB-1121
IM-BBB-11223
How is it possible ?
My query is:
UPDATE `certificate_log_uae`
SET `DeviceSerialNumberTemp` = REPLACE(LEFT(DeviceSerialNumberTemp,2),
LEFT(DeviceSerialNumberTemp,2).'-')
You can use the Substr() function to get substrings out from your input string, at various positions and lengths.
Since the length of the last substring is not fixed; we can simply specify the start position to slice the substring, and leave specifying the length parameter. It will consider the substring till the end of the overall string.
Now, just concatenate this substrings back using - appropriately.
Try the following:
UPDATE `certificate_log_uae`
SET `DeviceSerialNumberTemp` = CONCAT(SUBSTR(`DeviceSerialNumberTemp`, 1, 2),
'-',
SUBSTR(`DeviceSerialNumberTemp`, 3, 3),
'-',
SUBSTR(`DeviceSerialNumberTemp`, 6)
)
One approach would be to just build the final string you want using concatenation:
UPDATE certificate_log_uae
SET DeviceSerialNumberTemp = CONCAT(LEFT(DeviceSerialNumberTemp, 2),
'-',
SUBSTRING(DeviceSerialNumberTemp, 3, 3),
'-',
SUBSTRING(DeviceSerialNumberTemp, 6));
Demo
If you are using MySQL 8+ or later, then there is a very simple regex based solution using REGEXP_REPLACE:
SELECT
DeviceSerialNumberTemp,
REGEXP_REPLACE(DeviceSerialNumberTemp, '(.{2})(.{3})(.*)', '$1-$2-$3') AS output
FROM certificate_log_uae;
Demo
Try simple insert function:
select Slno, insert(insert(slno, 3, 0, '-'), 7, 0, '-') from tbl
Demo
To update values try:
update certificate_log_uae set
DeviceSerialNumberTemp = insert(insert(DeviceSerialNumberTemp , 3, 0, '-'), 7, 0, '-')
I have a table where I am attempting to take 3 database table values and reformat them in a single value. Here is the SQL statement that I have at the moment:
SELECT
CASE WHEN cb_cardtype = 'Discover Credit Card'
THEN 'DS'
END +
';' + RIGHT(cardnumbers,4) + ';' + LPAD(MONTH(planexpdate), 2, '0') +
'/' + LPAD(YEAR(planexpdate), 2, '0') AS account_billing_key
FROM my_table
So what I wanted to get as an output here would be:
DS;4242;07/14
The problem is that I am using the + to attempt this, which actually adds the values together. Rather, I understand that I need to use CONCAT() to merge the values. I am unclear about how I can pull the individual values and then concatenate them as desired.
If your query is otherwise correct, all you need to do is to wrap all the strings you want to concatenate - comma separated - inside a call to CONCAT;
SELECT
CONCAT(
CASE WHEN cb_cardtype = 'Discover Credit Card' THEN 'DS' END,
';',
RIGHT(cardnumbers,4),
';',
LPAD(MONTH(planexpdate), 2, '0'),
'/',
LPAD(YEAR(planexpdate), 2, '0')
) AS account_billing_key
FROM my_table
I'm don't have a lot of knowledge of MySql (or SQL in general) so sorry for the noobness.
I'm trying to update a bunch of String entries this way:
Lets say we have this:
commands.firm.pm.Stuff
Well I want to convert that into:
commands.firm.pm.print.Stuff
Meaning, Add the .print after pm, before "Stuff" (where Stuff can be any Alphanumerical String).
How would I do this with a MySql Query? I'm sure REGEXP has to be used, but I'm not sure how to go about it.
Thanks
Try something like this. It finds the last period and inserts your string there:
select insert(s, length(s) - instr(reverse(s), '.') + 1, 0, '.print')
from (
select 'commands.firm.pm.Stuff' as s
) a
To update:
update MyTable
set MyColumn = insert(MyColumn, length(MyColumn) - instr(reverse(MyColumn), '.') + 1, 0, '.print')
where MyColumn like 'commands.firm.pm.%'
Perhaps use a str_replace to replace commands.firm.pm to commands.firm.pm.print
$original_str = "commands.firm.pm.15hhkl15k0fak1";
str_replace("commands.firm.pm", "commands.firm.pm.print", $original_str);
should output: commands.firm.pm.print.15hhkl15k0fak1
then update your table with the new value...How to do it all in one query (get column value and do the update), I do not know. All I can think of is you getting the column value in one query, doing the replacement above, and then updating the column with the new value in a second query.
To update rows that end in '.Stuff' only:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column) - CHAR_LENGTH('.Stuff') )
, '.print'
, '.Stuff'
)
WHERE Column LIKE '%.Stuff'
To update all rows - by appending .print just before the last dot .:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column)
- CHAR_LENGTH(SUBSTRING_INDEX(Column, '.', -1))
)
, 'print.'
, SUBSTRING_INDEX(Column, '.', -1)
)
WHERE Column LIKE '%.%'