Solved

SQL Server - Text field update

Posted on 2013-01-08
4
346 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
[X]
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
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

622 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