Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL TRIM duplicated Training characters

Posted on 2010-08-20
6
382 Views
Last Modified: 2012-05-10
I have text strings that can look like any of these lines

String<br />String<br />String<br />String<br />
String<br /><br /><br /><br />
String<br />String<br /><br /><br />
String<br />String<br />String<br /><br />

What command would I uses to remove the trailing <br />'s

Thanks
0
Comment
Question by:darren-w-
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33484237
presuming that the HTML code does not contain any € symbol on it's own (it should be &euro; ...), this could do:
select replace(replace(rtrim( replace(replace(yourfield, ' ' , '€'), '<br />', ' '), ' ', '<br />'), '€', ' ')

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33484251
case when right(strCol,6)='<br />' then left(strCol,len(strCol)-6) else strCol end
0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33484266
If you are using sql server then,

Update table_name
set
field_name = left(field_name,len(field_name) - 6)

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33484311

create function dbo.stripTrailing(@str varchar(8000),@trailing varchar(20))
returns varchar(8000)
as
begin
while @str like '%' + @trailing
	set @str = left(@str,len(@str)-len(@trailing))
return @str
end
GO

select dbo.stripTrailing(strCol, '<br />') from tbl

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33484370
My first solution takes the first trailing br away, 2nd takes all trailing br's away.
To fix angelIII's multi-replace method:

select
replace(
      replace(
            replace(
                  rtrim(
                        replace(
                              replace(
                                    replace(
                                          strcol, '<br />', CHAR(255))
                                    , ' ' , CHAR(254))
                              , Char(255), ' ')
                        )
                  ,' ', char(255))
            ,char(254), ' ')
      ,char(255), '<br />')
from tbl
0
 
LVL 13

Author Closing Comment

by:darren-w-
ID: 33484576
I found it  easy to understand and implement, may have been difficult to understand from a beginners perspective though.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question