Solved

SQL Server - Text field update

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Format Date fields 11 65
Help with Data Warehouse / Data Marts 4 42
Datatable / Dates ? 4 33
T-SQL Query 5 25
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

710 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