Solved

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

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

863 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

24 Experts available now in Live!

Get 1:1 Help Now