Solved

How to update a value in a column

Posted on 2008-06-17
14
192 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
 

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
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.

 

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

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

919 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