Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove spaces

Status
Not open for further replies.

jayfox

Programmer
Dec 19, 2002
29
US
Hi, I'm trying to remove spaces and html tags from a text field. The functions work great when I test them on data I created but when I go to use them on the field where html data is pulled into they don't work. I can't get this function to remove the spaces. In return this function does not remove the spaces.


EX. @desc = <br> <br> This is a test <html>
Function:
CREATE FUNCTION dbo.fnremovespace (@desc varchar(4000))
RETURNS varchar(4000)
BEGIN
while charindex('>',@desc)>0 and substring(@desc,charindex('>',@desc)+1,1) = ' '
begin
set @desc = stuff(@desc,charindex('>',@desc)+1,1,'')
end
return(@desc)
end

This function should return:
<br><br>This is a test<html>
 
Take a look at the [red]Replace[/red] function:
Code:
Set @desc = Replace(@desc, ' ', '')
Set @desc = Replace(@desc, '<', '[')
Set @desc = Replace(@desc, '>', ']')
This code should (ie - haven't tested it) remove all spaces and change <> characters to [] characters.

HTH,
John
 
That would get rid of the spaces but it will also remove the spaces in between the text I'm trying to pull out. When complete @desc = This is a test. If I do a replace it will read Thisisatest
 
You could always try:

Code:
REPLACE(@DESC, '>  ', '>')
REPLACE(@DESC, '  <', '<')
REPLACE(@DESC, '> ', '>')
REPLACE(@DESC, ' <', '<')

That should change: <br> <br> This is a test <html>
to <br><br> This is a test <html>

You have to run it twice since there are two spaces (in your example) between > and This and between test and <. So first you need to eliminate the double spaces and then the single ones.

Once you get rid of the spaces, then eliminate the HTML.

-SQLBill


 
Just do these repeatedly until no rows are affected, but you'll have to watch out for these character patterns occuring inside strings.

REPLACE(@DESC, '> ', '>')
REPLACE(@DESC, ' <', '<')

-------------------------------------
A sacrifice is harder when no one knows you've made it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top