Solved

sql question

Posted on 2008-10-03
4
195 Views
Last Modified: 2010-03-19
I need an SQL that will change all rows of a field of a table that ends in 'CC' to 'C'

Start with this:
D215CC
End with this:
D215C
0
Comment
Question by:thenrich
  • 3
4 Comments
 
LVL 32

Expert Comment

by:bhess1
ID: 22637287
This will do it:

UPDATE MyTable
SET MyField = Left(MyField, Len(MyField)-1)
WHERE MyField LIKE '%CC'

Test by running this SELECT:

SELECT MyField, Left(MyField, Len(MyField)-1)
FROM MyTable
WHERE MyField LIKE '%CC'
0
 
LVL 5

Author Comment

by:thenrich
ID: 22637391
What would I do if I wanted to 'CT' to 'P' ?
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 500 total points
ID: 22637432
Something like this:

UPDATE MyTable
SET MyField = Left(MyField, Len(MyField)-2) + 'P'
WHERE MyField LIKE '_%CT'

Test by running this SELECT:

SELECT MyField, Left(MyField, Len(MyField)-2) + 'P'
FROM MyTable
WHERE MyField LIKE '_%CT'

NOTE:  The underscore is a tweak that ensures it is not replacing a record with only 'CT' in the field with a value of 'P'.
0
 
LVL 32

Expert Comment

by:bhess1
ID: 22637470
You also have an alternate possibility using the STUFF command.  This version works for any arbitrary value and replacement, and uses variables to hold the search and replace values:

DECLARE @SearchFor varchar(10)
DECLARE @ReplaceWith varchar(10)

SET @SearchFor = 'CT'
SET @Replace = 'P'

UPDATE MyTable
SET MyField = STUFF(MyField, Len(MyField) - Len(@SearchFor), Len(@SearchFor), @Replace)
FROM MyTable
WHERE MyField LIKE '%' + @SearchFor

Test this with the code:


DECLARE @SearchFor varchar(10)
DECLARE @ReplaceWith varchar(10)

SET @SearchFor = 'CT'
SET @Replace = 'P'

SELECT MyField, STUFF(MyField, Len(MyField) - Len(@SearchFor), Len(@SearchFor), @Replace)
FROM MyTable
WHERE MyField LIKE '%' + @SearchFor
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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