How to prevent entering text containing spaces in mySQL - 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 ;

Related

MySQL only replace a single character if a pattern of characters is not there

I need to be able to replace a single character as long as it's not a specific combination of other characters. For example, if I find a single ' in my TEXT value, I need to escape it; but if it's already escaped (\') I need to ignore it.
There could also be more complex variations such as \"\ that I simply want to replace with -.
I also need to see if there are \" entries, but make sure they aren't already \\" (double escaped).
I'm guessing the solution is REGEXP_REPLACE, but I don't know how to properly tell it to NOT replace a matching pattern if another pattern exists...
Thanks!

Mysql TRIM/REPLACE not working for my Query

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;

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 :)

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.

VBA Trim() function truncating text oddly!

I'm trying to trim extraneous white space at the end of a memo field in MS Access. I've tried doing it a number of ways:
1) an update query with the field being updated to Trim([fieldname]). For some reason, that doesn't do anything. The whitespace is still there.
2) an update using a Macro function in which the field contents are passed as a String and then processed using the Trim() function and passed back. This one is really bizarre, in that it seems to truncate the text in the field at completely random places (different for each record). Sometimes 366 characters, sometimes 312, sometimes 280.
3) same as above but with RTrim()
How can I possibly be messing up such a simple function?! Any help much appreciated. Would like to keep my hair.
-Sam
According to this article:
Both Text and Memo data types store only the characters entered in a field; space characters for unused positions in the field aren't stored.
As hypoxide suggested, they may not in fact be spaces
Edit
I suspect that the last character in the field is a carriage return or linefeed character. If this is the case, then Trim (or any variations of Trim - RTrim\LTrim) won't work since they only remove space characters. As 'onedaywhen' suggested in the comment, try using the ASC function to determine the actual character code of the last character in the memo field. You can use something like the following in a query to do this:
ASC(Right(MyFieldName,1))
Compare the result of the query to the Character Set to determine the actual character that ends the memo field. (Space = 32, Linefeed = 10, Carriage Return = 13).
You may have to test the last character and if it is a linefeed or carriage return remove the character and then apply the trim function to the rest of the string.
This may date me, but does Access have different character types for fixed vs. variable lengths? in SQL, CHAR(10) will always by 10 chars long, padded if necessary, while VARCHAR(10) will be 'the' size up to 10. Truncating a CHAR(10) will just put the blanks back.