Solved

sql question

Posted on 2008-10-03
4
187 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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