I try to remove all whitespaces inside a string. For this, I use TRIM() function. Unfortunately it doesn't work as expected, inner whitespaces (between 35 and 'A') remain untouched:
select TRIM('Hopkins 35 A Street') as Street
Column type is nvarchar. The funny thing is that this function works fine (using example from above) when executed on W3Schools (TRIM function example): https://www.w3schools.com/sql/func_sqlserver_trim.asp.
I can use replace on this string and replace ' ' into '' without a problem. I work on SQL Server 18.7.1 (2020)
if you use TRIM like this you are only removing leading and trailing spaces from a string. To remove also spaces in between you should change to:
select TRIM(' ' FROM 'Hopkins 35 A Street') as Street
UPDATE: if you meant to remove all spaces you should use
SELECT REPLACE('Hopkins 35 A Street', ' ', '')
TRIM is only intended to make a double space become a single one
"Trimming" means the removal of whitespace from the start and/or the end of a string value. It never means (and has never meant to mean) the removal of whitespace within a string value (enclosed by non-whitespace characters).
You can indeed use the TRIM function with a FROM in its argument to specify other characters than whitespace to trim. In that case, the TRIM function will remove the specified characters from the start and the end of the string, but not within the string (enclosed by other characters).
In other words: the specified characters will be treated as if they were whitespace as well, but specifying them so will not affect the trimming behavior/algorithm itself.
Check out the sample on Microsoft Docs:
SELECT TRIM( '.,! ' FROM ' # test .') AS Result;
produces this result: # test
TRIM function will only remove only the leading and tailing spaces in the data. It cannot remove all the spaces in the data. I mean it cannot remove all the spaces if there are any spaces in the data like 'Hello World'. TRIM cannot remove the space between the word Hello and World and make it look like 'HelloWorld'. If you want to remove all the spaces, you can use the REPLACE function. In the REPLACE function you can replace the space with any character/number/symbol. If you don't need any you can simply remove the space with ''. like
SELECT REPLACE('Hopkins 35 A Street', ' ', '')
I am using wildcards in a join statement to find when a specific phrase is contained in a text field. Originally, I tried something like:
[Text Field] LIKE '%[^0-9A-Z]' + [Phrase] + '[^0-9A-Z]%'
However, this only gives me results that contain the phrase inside of the text field surrounded by spaces or special characters. Instead I need it to be when the phrase is surrounded by a leading or trailing space, or if the phrase is at the beginning or end of text field. Does anyone have any idea on how to accomplish this? I have searched around to no avail.
Presumably, you are using SQL Server. Just add spaces to the beginning and end of the column:
' ' + [Text Field] + ' ' LIKE '%[^0-9A-Z]' + [Phrase] + '[^0-9A-Z]%'
I have a varchar column having values with extra space (one,two or many more) at leading trail of the character.
I tried to remove the spaces with all replace,rtrim,patindex,charindex but didn’t have a luck.
I think you are looking for this
UPDATE Table
SET Column_name = REPLACE(Column_name, ' ', '')
If it is really space try using LTRIM(RTRIM(Column_name)). Alternatively, use some other function to treat a non-printable character:
Use char2hexint to check what that value is:
SEL val1, CHAR2HEXINT(val1) AS char2hexint_val1
FROM
mytable;
val1 char2hexint_val1
9 3900
In this case '9' was the leading character.
You can also explore the COLLATE function for non-printable character detection too.
I need to replace multiple characters in a string. The result can't contain any '&' or any commas.
I currently have:
REPLACE(T2.[ShipToCode],'&','and')
Which converts & to and, but how do you put multiple values in one line?
You just need to daisy-chain them:
REPLACE(REPLACE(T2.[ShipToCode], '&', 'and'), ',', '')
One comment mentions "dozens of replace calls"... if removing dozens of single characters, you could also use Translate and a single Replace.
REPLACE(TRANSLATE(T2.[ShipToCode], '[];'',$#', '#######'), '#', '')
We used a function to do something similar that looped through the string, though this was mostly to remove characters that were not in the "#ValidCharacters" string. That was useful for removing anything that we didn't want - usually non-alphanumeric characters, though I think we also had space, quote, single quote and a handful of others in that string. It was really used to remove the non-printing characters that tended to sneak in at times so may not be perfect for your case, but may give you some ideas.
CREATE FUNCTION [dbo].[ufn_RemoveInvalidCharacters]
(#str VARCHAR(8000), #ValidCharacters VARCHAR(8000))
RETURNS VARCHAR(8000)
BEGIN
WHILE PATINDEX('%[^' + #ValidCharacters + ']%',#str) > 0
SET #str=REPLACE(#str, SUBSTRING(#str ,PATINDEX('%[^' + #ValidCharacters +
']%',#str), 1) ,'')
RETURN #str
END
If you need fine control, it helps to indent-format the REPLACE() nesting for readability.
SELECT Title,
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(RTRIM(Title),
' & ',''),
'++', ''),
'/', '-'),
'(',''),
')',''),
'.',''),
',',''),
' ', '-')
AS Title_SEO
FROM TitleTable
If you use SQL Server 2017 or 2019 you can use the TRANSLATE function.
TRANSLATE(ShipToCode, '|+,-', '____')
In this example de pipe, plus, comma en minus are all replaced by an underscore.
You can change every character with its own one.
So in the next example the plus and minus are replaced by a hash.
TRANSLATE(ShipToCode, '|+,-', '_#_#')
Just make sure the number of characters is the same in both groups.
Hope this might helps to anyone
If you want to replace multiple words or characters from a string with a blank string (i.e. wanted to remove characters), use regexp_replace() instead of multiple replace() clauses.
SELECT REGEXP_REPLACE("Hello world!123SQL$##$", "[^\w+ ]", "")
The above query will return Hello world123SQL
The same process will be applied if you want to remove multiple words from the string.
If you want to remove Hello and World from the string Hello World SQL, then you can use this query.
SELECT REGEXP_REPLACE("Hello World SQL", "(Hello|World)", "")
This will return SQL
With this process, the query will not look redundant and you didn't have to take care of multiple replace() clauses.
Conclusion
If you wanted to replace the words with blank string, go with REGEXP_REPLACE().
If you want to replace the words with other words, for example replacing & with and then use replace(). If there are multiple words to be replaced, use multiple nested replace().
Just looking for another way to remove any excess spaces(>1) throughout a query. I'm taking information for my Oracle query from an Excel spreadsheet so there's bound to be some excess zeroes due to user error and whatnot so that when I'm taking the information from the excel it will have 2 sometimes 3 extra spaces after some values so we won't get any records returned from Oracle when we query it through the application. I have tried Trim(ds.Tables.Item(0).Rows.Item(k).Item(i).ToString.ToUpper) but this isn't removing the extra spaces in some of the values. Is there some SQL statement I don't know of or perhaps another reason why Trim isn't working?
Edit: Was writing replace function incorrectly.
Original: string.Replace(" ", " ")
New: string = string.replace(" ", " ")
Use String.Replace(" ", String.Empty) instead. Trim only removes leading and trailing spaces.
On the Oracle side, you can use the REGEXP_REPLACE function, but you need to apply it to each column you want to remove the extra spaces from:
SELECT
REGEXP_REPLACE(myCol1, ' {2,}', ' ') AS myCleanCol1,
REGEXP_REPLACE(myCol2, ' {2,}', ' ') AS myCleanCol2,
FROM myTable
The example here looks for all occurrences of two or more spaces (that's the second parameter) and replaces them with a single space (the third parameter).
You can use REGEXP_REPLACE
Could be something like this:
regexp_replace(your_column, '\s{2,}', '')
Here is a sqlfiddle demo