SQl query to remove all characters to the right help needed

SQL 2008 database with three tables

today I found my database was corrupted with a lot of characters from my web site

it added <div style=............   many times on the record

I started deleting the info by hand but I have over 40000 records to go thru.

when I update the record manually I place the cursor on the line right before the <div then I hit CNTRL + Del keys and everything from the right is removed.


702<div style="display:none">online <a href="http://geekiest.net/post/250-awesome-photography-tutorials.aspx">read</a> redirect</div>

702 is all I want here

This is not positional the values prior to the <div are mixed lengths.

this is all on one column

is there a query that can do this


Hoping to save a lot of time
LVL 23
Thomas GrassiSystems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
Test first but this should do it.
update myTable set myColumn = left(myColumn, CHARINDEX('<', myColumn)-1)

Open in new window

ste5anSenior DeveloperCommented:

DECLARE @Text NVARCHAR(255) = N'702<div style="display:none">online <a href="http://geekiest.net/post/250-awesome-photography-tutorials.aspx">read</a> redirect</div>';

SELECT  CASE WHEN PATINDEX('%<div styl%', @Text) > 0 THEN SUBSTRING(@Text, 1, PATINDEX('%<div styl%', @Text) - 1)
             ELSE @Text

Open in new window

The test pattern must be long enough, otherwise it would mask valid content.

btw, this is not corruption, this is a bad development habit: Writing non-sanitized user-content to the database.
Mike EghtebasDatabase and Application DeveloperCommented:
if all 40000 records need to have 702 and there are only 40000 records in the table, then consider:

Uodate Table1 Set ColumnName = 702
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Thomas GrassiSystems AdministratorAuthor Commented:

first thanks for responding so quickly

the 702 was just an example not all records are 702

here is a sampling of the db see attached first 30 plus records are valid look at column B

Lee I ran your query
update dbo.recordings set RecordingTitle = left(RecordingTitle, CHARINDEX('<', RecordingTitle)-1)

got this error

Msg 537, Level 16, State 5, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function.
The statement has been terminated.

dsackerContract ERP Admin/ConsultantCommented:
Lee's post (being the first and correct) deserves the bulk of points, but you simply need to add a WHERE statement to the UPDATE.

update myTable set myColumn = left(myColumn, CHARINDEX('<', myColumn)-1)
WHERE CHARINDEX('<', myColumn ) > 0

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas GrassiSystems AdministratorAuthor Commented:
Thanks to every one

one simple query saved me hours of work
ste5anSenior DeveloperCommented:
Well, just be sure that no column had '0<1' as content before your "corruption".
Thomas GrassiSystems AdministratorAuthor Commented:

yes the database is all good now made a full backup and saved a copy elsewhere

it was not corrupt somehow my web site has a hole in it allowing that to happen need to investigate  that end

Just glad the site is working now.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.