Solved

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

Posted on 2014-03-17
4
1,184 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
[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
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 166 total points
ID: 39934934
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
ID: 39934938
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 40

Accepted Solution

by:
Kyle Abrahams earned 167 total points
ID: 39934940
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 40

Expert Comment

by:lcohan
ID: 39934956
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

705 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