Solved

SQL Server - Text field update

Posted on 2013-01-08
4
340 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

803 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