Solved

How to update a value in a column

Posted on 2008-06-17
14
194 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
  • 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 100 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 400 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

770 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