MySQL - How to get the string next after to a symbol? - mysql

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

Related

Remove last char if it's a specific character

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;
?>

REPLACE string in MySQL but only specified part

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

Count the occurrences of substring

I have a html string:
<img src="aaa"/>blah blah <img src="333"/>long content
I just want to count the number of img tag occurrences in the string field.
I know i can use CLR in SQL server or i could use something like HtmlAgilityPack to parse the string, but i have no permission to install CLR and want a more effective way.
So is there any way to use normal SQL to achieve this.
try this
Declare #string varchar(1000)
Set #string = '<img src="aaa"/>blah blah <img src="333"/>long content<img'
select len(#string) - len(replace(#string, '<img', 'xxx'))
One dirty trick you can use is,
select (len(myCol) - len(replace(myCol, '<img ', ''))) / len('<img ') AS imgCount
from myTable

mysql - how to replace a sub-string?

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

MySQL Query: Trim a string in an entry from some start string to some end string?

I want to write a query to remove '' strings from an entry up until '>' characters.
Say I have:
<a href='somesite'>Text Link</a>
The result should be:
Text Link</a>
You can try this:
SET #tmp = "<a href='somesite'>Text Link</a>";
SELECT RIGHT(#tmp,LENGTH(#tmp)-LOCATE('>',#tmp));
Note: LOCATE finds the first occurrence of substring; that may or may not what you want.
As Christopher and Dor noted, this is almost certainly better done in your code. Still, just for fun, here is how you might do it in MySQL.
SELECT SUBSTRING(`your_column`, LOCATE('>', `your_column`,
LOCATE('<', `your_column`)) + 1)
FROM `your_table`;