Solved

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

Posted on 2014-03-17
4
1,149 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
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 39

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

840 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