i would like to replace string in my query but only on the end of the string this is my example:
SET #exampleString = 'example_a_chapter_a';
SELECT REPLACE(#exampleString ,'_a','_b1');
Result what I get is this: example_b1_chapter_b1
But i would like this: example_a_chapter_b1
But there can be more time the '_a' in the string as this 'example_a_type_a_chapter_a', but
i would like to replace just the end '_a' of the string.
Thanks for you help
This will be tricky since MySQL can not replace by regex. One of possible ways is:
SELECT REPLACE(REPLACE(CONCAT(#exampleString, '#END'), '_a#END', '_b1'), '#END', '');
-i.e. add something that 100% is not in original string, to it's end and then replace.
Edit: second REPLACE is needed in case if original string doesn't end with _a (so you'll need to remove added #END)
You could not use replace() at all. Just reconstruct the string:
select (case when #exampleString like '%_a'
then concat(left(#exampleString, length(#exampleString) - length('_a')),
'_b1'
)
else #exampleString
end)
This has the advantage that it works even when the string doesn't end in '_a'.
Related
I need help in mysql query, i written this query
select * from post where content REGEXP '^q'
this query is working but it also includes spaces in filter, what i want to do if any content string like "qqqqqq" or "qqqq" or "qqq" or "qq" or "q" for this string only it should have to filter, right now what is happening if i have string like "qqqq qq" then also it is giving me the result, it should not consider that space, can anyone please help me to resolve this issue ?
You can fix your regexp like next:
select * from post where content REGEXP '^q+$';
This regular expression mean the string starts with q and contain only 1 or more q symbols till end of string
Test it on SQLize.online
Try Using this ^q+(?![\s])+$ Regular Expression.
Above RegExp will check for string starting with q and without space.
You don't really need a regex for this. String functions are likely to be more efficient. You can replace all "q"s with empty strings, and ensure that that resulting string is empty:
select * from post where replace(content, 'q', '') = ''
Note that this also allows the empty string. If you want to avoid that, then:
select * from post where content <> '' and replace(content, 'q', '') = ''
For Example: word_word_#_word_word
i want to replace third word from left after the #_ by someother word, i dont know the exact string from the database. So how can i write sql query for this.
use replace:
select SELECT REPLACE('word_word_#_word_word', '#_word', '#_otherword');
Use Stuff function
Declare #String varchar(200)='word_word_#_word_word'
SELECT #String AS GivenString,STUFF(#String,(CHARINDEX('#_',#String)+2),0,'Otherword'+'_') AS Otherword_Column
OutPut
GivenString Otherword_Column
------------------------------------------------------
word_word_#_word_word word_word_#_Otherword_word_word
I need to update values in a table by removing their last char if they ends with a +
Example:
John+Doe and John+Doe+ should both become John+Doe.
What's the best way to achieve this?
UPDATE table
SET field = SUBSTRING(field, 1, CHAR_LENGTH(field) - 1)
WHERE field LIKE '%+'
If you are trying to display the field instead of update the table, then you can use a CASE statement:
select
case
when right(yourfield,1) = '+' then left(yourfield,length(yourfield)-1)
else yourfield end
from yourtable
SQL Fiddle Demo
you didn't explain exactly the situation.
but if you search for names in a text. I'll remove all the non chars (anything not a-z and A-Z) including spaces and then compare.
if you want just the last char, try the SUBSTRING_INDEX function.
if you are passing to the DB as a string, you can do this with str_replace
<?php
$str = "John+Doe+";
$str = str_replace("+"," ",$str);
echo $str;
?>
My table has a column with a JSON string that has nested objects (so a simple REPLACE function cannot solve this problem) . For example like this: {'name':'bob', 'blob': {'foo':'bar'}, 'age': 12}. What is the easiest query to append a value to the end of the JSON string? So for the example, I want the end result to look like this: {'name':'bob', 'blob': {'foo':'bar'}, 'age': 12, 'gender': 'male'} The solution should be generic enough to work for any JSON values.
What about this
UPDATE table SET table_field1 = CONCAT(table_field1,' This will be added.');
EDIT:
I personally would have done the manipulation with a language like PHP before inserting it. Much easier. Anyway, Ok is this what you want? This should work providing your json format that is being added is in the format {'key':'value'}
UPDATE table
SET col = CONCAT_WS(",", SUBSTRING(col, 1, CHAR_LENGTH(col) - 1),SUBSTRING('newjson', 2));
I think you can use REPLACE function to achieve this
UPDATE table
SET column = REPLACE(column, '{\'name\':\'bob\', \'blob\': {\'foo\':\'bar\'}, \'age\': 12}', '{\'name\':\'bob\', \'blob\': {\'foo\':\'bar\'}, \'age\': 12, \'gender\': \'male\'}')
Take care to properly escape all quotes inside json
Upon you request of nested json, i think you can just remove last character of the string with SUBSTRING function and then append whatever you need with CONCAT
UPDATE table
SET column = CONCAT(SUBSTRING(column, 0, -1), 'newjsontoappend')
modify Jack's answer. Works perfectly even column value is empty on first update.
update table
set column_name = case when column_name is null or column_name =''
then "{'foo':'bar'}"
else CONCAT_WS(",", SUBSTRING(column_name, 1, CHAR_LENGTH(column_name) - 1),SUBSTRING("{'foo':'bar'}", 2))
end
a field is a varchar, i want to replace a sub string of this field.
ab123fg => ab456fg
the above is just a example.
just like the sub string to be replaced.
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace
REPLACE(str, 'ab123fg ', 'ab456fg')
EDIT:
MySQL doesnt have a find and replace regex function, maybe have a look at UDFs