Take a look : FIDDLE
select IF((TRIM(replace(' IKECHUKWU OSUJI',' ',''))=TRIM(replace('IKECHUKWU OSUJI','
',''))),"same","diff");
select IF((TRIM(replace(' Aman Minhas ',' ',''))=TRIM(replace(' Aman Min has','
',''))),"same","diff");
The first query returns diff. The second returns same. Its some weird spacing issue, cant seem to understand why this behaviour.
Your first string has a tab in it:
select IF((TRIM(replace(' IKECHUKWU OSUJI',' ',''))
^ this is actually a tab in the Fiddle
You can get rid of it with an additional REPLACE:
REPLACE(REPLACE(myString, ' ', ''), '\t', '')
The \t is a special literal. Other special literals such as newline or ASCII NUL may impact you as well. Literals are listed here.
Related
A column has some string value with lot of spaces and tabs. i am unable to trim these spaces and white spaces.
e.g select trim('column_name') from table_name;
I am not sure how much space is in the string . it may 2 for one string and 12 for other. so replace() is also not working.
I got the answer by using the following:
trim(replace(convert(column_name USING ascii),'?',''))
MySQL TRIM() does not affect tabs unless they are specified. If you have multiple mixed tabs and spaces, you might have to replace the tabs with spaces and then trim the results.
SELECT TRIM(REPLACE(column_name, CHAR(9), ' ')) FROM table_name;
Given the value of name column: AL TAIRAWI TRADING ESTABLISHMENT
Update ae_companies set uniqueidentifier = CONCAT(trim(LEFT(name, 5)), '_', id, '_ae')
Above query produces this:
AL TA_6_ae
What I am looking for is this:
ALTA_6_ae
I have no idea why the trim() isn't working here.
Use replace() instead of trim().
Update ae_companies
set uniqueidentifier = CONCAT(replace(LEFT(name, 5), ' ', ''), '_', id, '_ae')
TRIM() isn't working (doing the job you thought it does) in your case because its description is:
Remove leading and trailing spaces
When you do LEFT(name, 5) which produces AL TA all leading and trailing spaces are removed when you wrap this around TRIM() the way you did. In your case space character is neither at the last nor at the first position, so this is why your result doesn't change when trimmed.
What you're looking for is a REPLACE($INPUT_STRING, ' ', '') to truncate all occurences of space character within the input string.
I'm trying to remove parenthesis from the area code of a number. I'm able to do so but when I try to concatenate the two Replace functions, the numbers repeat with only one parenthesis removed.
This is what I tried so far:
SELECT HomePhone, REPLACE(HomePhone, '(', '') +
REPLACE(HomePhone, ')', '') AS Expr1
FROM dbo.Employees
http://i.imgur.com/4iJoFzE.png
Nest don't add
Replace(Replace(HomePhone,')',''),'(','')
Look at how the function replace works. It expects string With Text To Evaluate, string to replace, string to replace with)
By adding them you should be getting the number listed twice, but if the data type isn't long enough it may be truncating values. by nesting you're telling the system to replace the ) and then use that string w/o the ) to have the ( replaced with ''.
You cannot concatenate in this way, you must use the concat function in SQL. Or use thus:
SELECT HomePhone, REPLACE(REPLACE(HomePhone, ')', ''), '(', '') AS Expr1 FROM dbo.Employees
I have some sentences in string in MySQL. And I need to replace substrings such as 'My' to 'my' if this word not first in sentence. How I can doing this?
CHAR, REPLACE, REPEAT, etc. I'd recommend reading mySQL ref: http://dev.mysql.com/doc/refman/5.5/en/string-functions.html
If you just want to replace several words, you can replace them, using this approach:
UPDATE str_test SET str = REPLACE(str, ' My', ' my')
fiddle
As the words inside the text will be preceded by space. But if you want a regexp replace, it will be a more difficult task:
How to count words in MySQL / regular expression replacer?
https://dba.stackexchange.com/questions/15250/how-to-do-a-case-sensitive-search-in-where-clause
MySql support for string is very limited. A quick solution would be to use something like this:
SELECT
CONCAT(
LEFT(col, 1),
REPLACE(SUBSTRING(col, 2), 'My', 'my')
)
Please see fiddle here. This will replace all the strings My to my, except the first one.
Or maybe this:
SELECT
col,
RTRIM(REPLACE(CONCAT(col, ' '), ' My ', ' my '))
FROM
yourtable
that will replace all whole words except the first one.
I use a CONVERT function in my SELECT statement in order to avert utf8 errors, but MySQL leaves question marks behind. Is there a way to convert the unconvertable characters to blank or space characters?
SELECT MeetId,
ResId,
Special,
CONVERT(proposal USING ascii) as Proposal,
Analysis,
Vote,
Vote_for,
Oppose,
Discret,
Abstain,
gpVote %s
FROM RESO
WHERE RESO.MeetId = %s
As an example a typical result may have this in a field: 'The current issue ?A? is on the table '
What about just using REPLACE:
SELECT
REPLACE(CONVERT('§123' USING ascii), '?', '')
And the Fiddle.
Good luck.
Be careful, sgeddes solution also removes all question marks(if exist) from your string!
For example :
SELECT REPLACE(CONVERT('§How are you?' USING ascii), '?', '')
Output will be : How are you