Solved

SQL Server - Text field update

Posted on 2013-01-08
4
334 Views
Last Modified: 2013-01-08
Hi,

I have a table of approx 1000 records in SQL Server 2008 and I need to update a text field containing email addresses.  I need to remove all text to the right of, and including, the first # symobol and retain the left hand part of the address.  

 account@bsigroup.com#mailto:account@bsigroup.com#

Thanks
0
Comment
Question by:qjump
4 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38755428
Try something like this:

Update yourtable
set email_address = substring('account@bsigroup.com#mailto:account@bsigroup.com#', 1, charindex('#', 'account@bsigroup.com#mailto:account@bsigroup.com#')-1)

Open in new window


(Playing with using a variable instead of hard coding the string twice, but it isn't working yet, but thought I'd get this your way for starters)
0
 
LVL 13

Expert Comment

by:LIONKING
ID: 38755438
You can try something like this:

UPDATE yourTable
SET yourField = SUBSTRING(yourField,1,CHARINDEX('#',yourField)-1)

To test first, you can try a simple select:

SELECT yourField, SUBSTRING(yourField,1,CHARINDEX('#',yourField)-1)

And adjust accordingly.

Hope it helps.
0
 
LVL 8

Expert Comment

by:Crashman
ID: 38755452
here

DECLARE @tbl TABLE (field VARCHAR(150))

insert INTO @tbl (field) VALUES ('account@bsigroup.com#mailto:account@bsigroup.com#')

SELECT field FROM @tbl t

UPDATE @tbl SET field = LEFT(field,CHARINDEX('#',field)-1) 

SELECT field FROM @tbl t

Open in new window

0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 295 total points
ID: 38756004
BTW, the above approaches will all throw errors if you have any values without a # character.  Two ways to guard against that:

UPDATE tbl
SET col = LEFT(col, CHARINDEX('#', col) - 1)
WHERE col LIKE '%#%

Open in new window


or:

UPDATE tbl
SET col = LEFT(col, CHARINDEX('#', col + '#',) - 1)

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

747 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

9 Experts available now in Live!

Get 1:1 Help Now