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).
Related
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 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;
I have this column in my table with shelf names
M2-8-3
N1-12-1
N1-3-8
N2-6-1
O1-11-3
O2-1-5
O2-10-1
O2-10-2
O2-10-6
O2-16-7
O2-17-6
O2-17-7
O2-4-2
When i order by it ascending it gives the result visible above but how could i write the query to get the result visible below
M2-8-3
N1-3-8
N1-12-1
N2-6-1
O1-11-3
O2-1-5
O2-4-2
O2-10-1
O2-10-2
O2-10-6
O2-16-7
O2-17-6
O2-17-7
The difference is in the position of N1-3-8 and O2-4-2
Try this:
SELECT col
FROM yourTable
ORDER BY SUBSTRING_INDEX(col, '-', 1),
SUBSTRING(SUBSTRING_INDEX(col, '-', -2),
1,
INSTR(SUBSTRING_INDEX(col, '-', -2), '-') - 1),
SUBSTRING_INDEX(col, '-', -1)
This is the brute force approach, where each of the three terms in the ORDER BY clause corresponds to a portion of your hyphenated string.
This may be a slightly simpler method:
order by substring_index(col, '-', 1),
substring_index(col, '-', -2) + 0,
col
This ends up converting the middle portion to a number for sorting purposes.
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 '%.%'
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 :).