[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

How to update a value in a column

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
maloriopolium
Asked:
maloriopolium
  • 6
  • 4
  • 4
2 Solutions
 
howyueCommented:
UPDATE Schedule SET MyColumn ='ZZZZ'
WHERE MyColumn LIKE 'ABCD%'
0
 
appariCommented:
Update Schedule
Set MyColumn = 'ZZZZ' + SubString(MyColumn , 5, datalength(MyColumn)-4)
where MyColumn like 'ABCD%'
0
 
appariCommented:
ignore my comment, I thought you want to replace the part ABCD with ZZZZ if the value starts with ABCD.
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
maloriopoliumAuthor Commented:
Sorry, what I meant was I want to keep the stuff after ABCD. For example: ABCD12345 becomes ZZZZ12345.

Thanks
0
 
howyueCommented:
UPDATE Schedule SET MyColumn =REPLACE(MyColumn, 'ABCD', 'ZZZZ')
WHERE MyColumn LIKE 'ABCD%'
0
 
maloriopoliumAuthor Commented:
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
 
howyueCommented:
what is the datatype of ur [File] ?

UPDATE Schedule SET File =REPLACE(File , 'ABCD', 'ZZZZ')
WHERE File LIKE 'ABCD%'
0
 
maloriopoliumAuthor Commented:
The dat type is just text
0
 
appariCommented:
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
 
howyueCommented:
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
 
howyueCommented:
sorry not remarks is [File]

UPDATE Schedule SET [File] = REPLACE(CONVERT(VARCHAR(MAX), [File]), 'ABCD', 'ZZZZ')
WHERE [File] LIKE 'ABCD%'
0
 
maloriopoliumAuthor Commented:
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
 
howyueCommented:
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
 
appariCommented:
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now