Solved

How can I select and then update certain characters of a string in sql server 2005?

Posted on 2014-03-17
4
1,107 Views
Last Modified: 2014-03-17
Hi

Suppose I have a column with data like

256dddggg@@576576
dgg782t@@6786
cb8979000shdj@@89789789
cn44@@123

or any other combination.

I need to first select this part   @@576576 and remove the @@ so that I am left with  576576. Then I want to select everything before @@ and update the column with whats left hence removing everything after @@.

Hope this makes sense.
0
Comment
Question by:jazz__man
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 166 total points
Comment Utility
You mean like this?

select LEFT('cb8979000shdj@@89789789', charindex('@@','cb8979000shdj@@89789789')-1)


If that's what you need just replace 'cb8979000shdj@@89789789' with any other string you posted above to test it then with the actual column name which you can use in an UPDATE statement like:

update tablename
set columnname = LEFT(columnname, charindex('@@',columnname)-1)
0
 
LVL 38

Assisted Solution

by:Jim P.
Jim P. earned 167 total points
Comment Utility
This should do it for you:

select	left(colName, CHARINDEX(CHAR(64),colName,1)-1) -- CHAR(64) = @
from	MyTable

Open in new window


or to update
Update  MyTable
set        colName = left(colName, CHARINDEX(CHAR(64),colName,1)-1) -- CHAR(64) = @
where  colName like '%' + CHAR(64) + '%'

Open in new window

0
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 167 total points
Comment Utility
declare @v varchar(50)
set @v = 'dgg782t@@6786'

select substring(@v, 1, charindex('@',@v) - 1) LEFTOF,
substring(@v, charindex('@@',@v) + 2, len(@v) - charindex('@@',@v) + 2) RIGHTOF
0
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
Also what do you want to do with the characters AFTER the @@? You say "I need to first select this part   @@576576 and remove the @@ so that I am left with  576576." but not what to do with those and here's how you can get that data out of any of the strings you posted above -or columnname:

select substring('cb8979000shdj@@89789789', charindex('@@','cb8979000shdj@@89789789')+2, LEN('cb8979000shdj@@89789789')-2)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 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

11 Experts available now in Live!

Get 1:1 Help Now