Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to update a value in a column

Posted on 2008-06-17
14
Medium Priority
?
199 Views
Last Modified: 2010-03-20
Hi,

I have a table called Schedule and I would like to update a column called MyColumn. I want all strings that start with ABCD to be replaced by ZZZZ. I am using MS SQL Server. Can anybody help me do this?

Thanks
0
Comment
Question by:maloriopolium
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
14 Comments
 
LVL 2

Expert Comment

by:howyue
ID: 21810037
UPDATE Schedule SET MyColumn ='ZZZZ'
WHERE MyColumn LIKE 'ABCD%'
0
 
LVL 39

Expert Comment

by:appari
ID: 21810040
Update Schedule
Set MyColumn = 'ZZZZ' + SubString(MyColumn , 5, datalength(MyColumn)-4)
where MyColumn like 'ABCD%'
0
 
LVL 39

Expert Comment

by:appari
ID: 21810045
ignore my comment, I thought you want to replace the part ABCD with ZZZZ if the value starts with ABCD.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:maloriopolium
ID: 21810176
Sorry, what I meant was I want to keep the stuff after ABCD. For example: ABCD12345 becomes ZZZZ12345.

Thanks
0
 
LVL 2

Expert Comment

by:howyue
ID: 21810188
UPDATE Schedule SET MyColumn =REPLACE(MyColumn, 'ABCD', 'ZZZZ')
WHERE MyColumn LIKE 'ABCD%'
0
 

Author Comment

by:maloriopolium
ID: 21810215
Thanks howyue,
I have a problem. The column called MyColumn is actually called File
I get an error saying argument 1 of Replace function is not valid text? Any ideas
0
 
LVL 2

Expert Comment

by:howyue
ID: 21810251
what is the datatype of ur [File] ?

UPDATE Schedule SET File =REPLACE(File , 'ABCD', 'ZZZZ')
WHERE File LIKE 'ABCD%'
0
 

Author Comment

by:maloriopolium
ID: 21810260
The dat type is just text
0
 
LVL 39

Assisted Solution

by:appari
appari earned 400 total points
ID: 21810269
did you try the sql from my comment?

try this

Update Schedule
Set MyColumn = 'ZZZZ' + SubString(MyColumn , 5, datalength(MyColumn)-4)
where MyColumn like 'ABCD%'
0
 
LVL 2

Expert Comment

by:howyue
ID: 21810298
ur column is text, ok thats where the problem come from, REPLACE does not accept text as argument, however u can:
UPDATE Schedule SET [File] = REPLACE(CONVERT(VARCHAR(MAX), [File]), 'ABCD', 'ZZZZ')
WHERE Remarks LIKE 'ABCD%'

Open in new window

0
 
LVL 2

Accepted Solution

by:
howyue earned 1600 total points
ID: 21810305
sorry not remarks is [File]

UPDATE Schedule SET [File] = REPLACE(CONVERT(VARCHAR(MAX), [File]), 'ABCD', 'ZZZZ')
WHERE [File] LIKE 'ABCD%'
0
 

Author Comment

by:maloriopolium
ID: 21810573
Ok, what I ended up doing was changing the datatype from text to varchar. Now it works.

Just out of curiosity. What is the difference between varchar and text and which should be used when?

Thanks
0
 
LVL 2

Expert Comment

by:howyue
ID: 21810621
the essential different is that the maximum of their storage size. beside that, i'm not so sure. but in BOL microsoft suggests:

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead. For more information, see Using Large-Value Data Types.
0
 
LVL 39

Expert Comment

by:appari
ID: 21810677
and you better avoid replace, if you want to replace only the first occurance of ABCD.
replace will will replace all instances of ABCD in your text.

suppose your column has value "ABCDTest ABCD replace"
replace(yourCol,'ABCD', 'ZZZZ') will result "ZZZZTest ZZZZ replace"
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

636 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