Solved

SQL TRIM duplicated Training characters

Posted on 2010-08-20
6
379 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 142

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now