Exact string search with LIKE - mysql

I have a table column that includes string like:
1,3,10,15
or
2,1,4,5...
Amount of numbers can change. I need a regular expression that will bring me the column that includes exactly "1" for the first string "1,3,10,15".
It shouldn't choose it because of 10 or 15, so "3, 10, 15" for this string, it shouldn't be selected.

If you really want to do this with a like, try this:
Say your column is named numstring.
"," ||numstring||"," like "%,1,%"

Just use:
WHERE (colname LIKE '1,%' OR colname = '1')
as
SELECT * FROM
(
SELECT '1,2,3' as teststring
) as test
WHERE teststring LIKE '1,%'
;
returns the row "1,2,3"
and
SELECT * FROM
(
SELECT '12,22,23' as teststring
) as test
WHERE teststring LIKE '1,%'
;
returns nothing as expected

Related

Replace characters in a SELECT only if string ends with certain pattern in MySQL?

I want to replace a certain string with a string when doing a SELECT y MySQL, but only if the selected value ends with a specific string, but I don't know even if that's possible in MySQL.
For example, I need to select a field and replace "SL" with "S.L.", but only if the string ends with " SL", so values would be transformed like this:
"NAME SL" by "NAME S.L."
"SLACK SL A" by "SLACK SL A"
"NORMAL NAME" by "NORMAL NAME"
You can use case, but I don't think you want replace(). Instead:
select (case when str like '% SL'
then concat(left(str, length(str) - 2), 'S.L.')
else str
end)
select (case when COLUMN_NAME LIKE '%SL' then REPLACE(COLUMN_NAME,'SL', 'S.L')
else COLUMN_NAME
end) from TABLE_NAME

MySQL query for string replacement and set String

I have string data in one cell and column type text.
like example :
Q: {"newColumnName":"xxxx","filterList":[],"columnNames":"pqrs"}
columnNames,filterList,newColumnName are not in fixed position may be change.
i need to replace this above string data to
Need Result:{"columnNames":["pqrs"],"filterList":[],"parameters":{"newColumnName":"xxxx"}}
means i need to search and replace :-
1. "columnNames":"pqrs"------>"columnNames":["pqrs"].
2. "newColumnName":"xxxx" ------>"parameters":{"newColumnName":"xxxx"}.
pqrs and xxxx known to me. It can any string in there.
you can do it with a query like this:
It is ONLY a text replacement
to check
SELECT t.val
, REPLACE(
REPLACE(t.val,'"columnNames":"pqrs"','"columnNames":["pqrs"]')
, '"newColumnName":"xxxx"' , '"parameters":{"newColumnName":"xxxx"}') as result
FROM tt t;
to replace
UPDATE tt
set tt.val =
REPLACE(
REPLACE(tt.val,'"columnNames":"pqrs"','"columnNames":["pqrs"]')
, '"newColumnName":"xxxx"' , '"parameters":{"newColumnName":"xxxx"}');

Substring_index in Mysql should return blank string if delimeter not found

E:g
Case 1: delimeter not exist
string = "abcdefg"
mysql> select substring_index(string,'z',1) from table;
output: abcdefg
expected output : blank string ""
Case 2: delimeter exist
string = "abczdefg"
mysql> select substring_index(string,'z',1) from table;
output: abc
expected output : abc (same as substring returns)
I want this select query should return me blank string if 'z'(delimeter) is not exist
and if delimeter exist in string then whatever substring_index is regular functionality
what modification in Query i can do for my expected output ?
Substring_index does exactly what it should :)
For your task you can use substring
set #str = "abcdef";
select substring(#str, 1, LOCATE('z', #str) - 1) from dual;
That will do exactly what you want

How to replace only few characters from a string in mysql?

I have a field urn_sem.studentid that I'd like to replace a few characters in; for example:
ABC/2011/BCOMH_NC/I/12 → ABC/2011/BCOMH/I/12
ABC/2011/BCOMH_NC/I/24 → ABC/2011/BCOMH/I/24
I've tried this query:
SELECT REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
FROM urn_sem
but it doesn't show the new value.
Do you want this:
update urn_sem
set studentid = REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
where studentid like '%KNC/2011/BCOMH_NC/%'
The WHERE clause is optional. It ensures that the replace is only on rows that change.
And this sample query does not work?
SELECT REPLACE (studentid, '_', '') FROM urn_sem

MySql: updating a column with the column's content plus something else

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 '%.%'