Mysql TRIM/REPLACE not working for my Query - mysql

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;

Related

How to prevent entering text containing spaces in mySQL

It happens occasionally that users erroneously enter text with a trailing space in a text column, which is hard to spot visually. This can later cause problems when this text field has to be matched against another where the trailing space is not present. Is it possible in mySQL to enforce that a text string cannot contain a certain character (space in this case)?
Thankful for feedback!
There are a number of ways to achieve what you want:
Check the user input in your application and reject it if it contains a space. If your primary worry is the quality of the user inputs, then this is probably the best way to do this.
You can remove spaces (or just starting / trailing spaces) from the user input either in the application logic or using sql.
If you opt for removing all spaces from the user input in sql, then use the replace() function. If you just want to remove the starting and trailing spaces, then use the trim() function to achieve the desired results.
Using mysql function a simple way is based on trim()
select
trim(' try with trim ')
, length (trim(' try with trim '))
, length (' try with trim ')
from dual ;

How to replace delimiters from a string in SQL Server

I have the following data
abc
pqr
xyz,
jkl mno
This is one string separated by delimiters like space, new line, comma, tab.
There could be two or more consecutive spaces or tabs or any delimiter after or before a word.
I would like to be able to do the following
Get the individual words removing all leading and trailing delimiters off it
Append the individual words with "OR"
I am trying to achieve this to build a T-SQL query separated by OR clause.
Thanks
I think you can achieve what you need (although I think using a programming language is way better) using just SQL, here is my approach.
Kindly note that I will just handle commas, newlines and multiple-spaces, but you can simple follow using the same technique to remove the rest of your undesired characters
so let's assume that we have a table names ExampleData with a column named DataBefore and another called DataAfter.
DataBefore: has the line value that you want to clean
DataAfter: will host the cleaned text
First we need to trim the preceding & leading space(s) from the text
Update ExampleData
set DataAfter = LTRIM(RTRIM(DataBefore))
Second, we should clean all the commas, and replace them with spaces (doesn't matter if we will end up with many spaces together)
Update ExampleData
set DataAfter = replace(replace(DataAfter,',',' '),char(13),' ')
This is the part in which you may continue and remove any other characters using the same technique, and replace it by a space
So far we have a text that has no spaces before or after, and every comma, newline, TAB, dash, etc character replaced by a space, let's continue our cleaning procedure.
We can now safely move on to replace the spaces between words with just one, this is made by using the following SQL statement:
Update ExampleData
set DataAfter = replace(replace(replace(DataAfter,' ','<>'),'><',''),'<>',' ')
as per your needs, we need to place an OR between each word, this is achievable with this SQL statement:
Update ExampleData
set DataAfter = replace(replace(replace(DataAfter,' ','<>'),'><',''),'<>',' OR ')
we are done now, as a final step that may or may not make a change, we need to remove any space at the end of the whole text, just in case an unwanted character was at the end of the text and as a result got replaced by a space, this can be achieved by the following statement:
Update ExampleData
set DataAfter = RTRIM(DataAfter)
we are now done. :)
as a test, I've generated the following text inside the DataBefore column:
this is just a, test, to be sure, that everything is, working, great .
and after running the previous commands, ended up with this value inside the DataAfter column:
this OR is OR just OR a OR test OR to OR be OR sure OR that OR everything OR is OR working OR great OR .
Hope that this is what you want, let me know if you need any extra help :)

SQL Selecting Values not working

I have a chemistry database (very small, about 60kb or so) in a mysql database. My
select * from firstdatabase;
works fine with any column but symbol. When I do
select * from firstdatabase where symbol = "Y";
for example, I get an empty set. However, when I do
select symbol from firstdatabase;
I get every symbol in the database including "Y". This problem doesn't occur with any other field except symbol. I have also tried it with double lettered and triple lettered elements to no avail. Please help?
My first guess is your symbol column contains whitespace characters. So you should try TRIM function.
select * from firstdatabase where TRIM(symbol) = "Y"
Use LTRIM and RTRIM if there whitespace characters in your symbol column.You should try LTRIM and RTRIM function.
select * from firstdatabase where LTRIM(RTRIM(symbol))= 'Y'

Removing non-breaking spaces?

I have a query for remove all special characters.
But ONE space resists to that query at the end of email string.
Example : 'test#hotmail.com '
UPDATE my_table SET email= REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM(LTRIM(RTRIM(email))),\'\x0B\',\'\'),\'\0\',\'\'),\'\t\',\'\'),\'\r\',\'\'),\'\n\',\'\'),\'\r\n\',\'\'),\'\n\r\',\'\'),\' \',\'\'),CHAR(160),\'\') WHERE id=X
Why?
I use this statement because I have a WHERE id IN(), so I don't want to process special characters in PHP. I want to UPDATE every emails directly with SET and replace, trim() function.
However, some whitespace is not deleted and I don't know why.
My table has approximately 12 millions of rows. I have programmed a CRON which fetch them to delete all specials characters (unfortunately because in the past we don't had check them on INSERT).
So I have build this query to process my 12 MM rows. It works very great except the right whitespace (sometimes it is removed sometimes not). And I want to add that on Workbench, the query works 100% all the time. It does not make sense.
Here is my query again without backslash and with my where IN:
UPDATE NEWSLETTER_SUBSCRIPTION SET email= REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM(LTRIM(RTRIM(email))),'\x0B',''),'\0',''),'\t',''),'\r',''),'\n',''),'\r\n',''),'\n\r',''),' ',''),CHAR(160),'') WHERE id IN (' . implode(',', $idEmailToBeProcess) . ')
$idEmailToBeProcess contains around 500 ids.
I think the right whitespace it's a non-breaking space, but my last test with CHAR(160) in my query didn't work.
how about whitelisting? ie allow only valid characters
regex_replace [^-_.#a-zA-Z] with ''
Ok, finally I had found the problem !!!
Encoding of PDO is the problem...
Just adjusted driver options and all works good!
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'')
Thanks guys anyway!
Found that on MySQL checking against CHAR(160) does not work.For UTF-8 NBSP the below worked for me:
REPLACE(the_field, UNHEX('C2A0'), ' ').
Solution provided on similar stack overflow question Whitespace in a database field is not removed by trim()

MySQL - remove whitespace before and after a given string or char

Is it possible to update a row by removing the whitespace (1 or more spaces) before and after a given string or char? I need all spaces before and after a specific char (#) to be stripped but also leave the other spaces in the cell intact.
Example:
'This is a simple # example'
should be updated to
'This is a simple#example'
likewise:
'This is another #example'
should be updated to
'This is another#example'
I can do this using PHP but it would be much easier if there was a way to have this done in a single query.
There is no default regex you can use with replacing. So the options you have are:
extend MySQL with a regex-replace:
You could use a UDF (user defined function, this one to be exact) to do a regex replace. You can then use something like \s*#\s* to indicate multiple spaces around an #.
Use the default replace:
you'd need to 'hardcode' your replaces to account for several spaces before and after the #, a cumbersome (and never complete) task. You'd end up with a lot of replaces, or a repetition of a certain function (remove 100 times one space from the # sign). To be clear, the mentioned doubble replace in the comments and #luksch' answer will not suffice for more then 1 space.
What about this:
SELECT REPLACE(REPLACE(line, ' #', '#'), '# ', '#') FROM tab1;
sqlfiddle
UPDATE:
to allow for the removal of any number of spaces before or after the # do this:
SELECT CONCAT(RTRIM(LEFT(line,LOCATE('#',line)-1)),'#',
LTRIM(SUBSTR(line,LOCATE('#',line)+1)))
FROM tab1;
see the new sqlfiddle to play around with this.
Note that if you have more than one # in the line this method only works for the first.