Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 32
TSQL - How to declare table name 26 28
insert wont work in SQL 14 18
transaction in asp.net, sql server 6 31
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
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.

813 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

18 Experts available now in Live!

Get 1:1 Help Now