26

I have a table within a SQL Server 2008 database called Meter. This table has a column called Name.

Each entry within the column Name has a prefix of the following ZAA\. I'd like to change this prefix to ZAA_ without affecting the rest of the text within the column.

4

3 に答える 3

30
UPDATE Meter
SET Name = 'ZAA_' + SUBSTRING(Name, 4, LEN(Name))
WHERE SUBSTRING(Name, 1, 4) = 'ZAA\'

編集:

または、@ Damien_The_Unbliever が述べているように、インデックスを使用するには:

UPDATE Meter
SET Name = 'ZAA_' + SUBSTRING(Name, 4, LEN(Name))
WHERE Name LIKE 'ZAA\%'

編集

あなたのコメントから、次のステートメントを試して、追加の問題を修正してください\

UPDATE Meter
SET Name = 'ZAA_' + SUBSTRING(Name, 5, LEN(Name))
WHERE Name LIKE 'ZAA_\%'
于 2013-09-02T10:32:09.740 に答える
2

ここにSQLFiddelのデモがあります

以下は、試すことができるクエリです

CREATE TABLE Meter
    ([Name] varchar(7))
;

INSERT INTO Meter
    ([Name])
VALUES
    ('ZAA\001')
;


select * from Meter;

Update Meter
   set Name = stuff(Name,4,1,'_')
 Where SUBSTRING(Name, 1,4) ='ZAA' + Char(92);

select * from Meter;
于 2013-09-02T10:32:39.170 に答える
2

マイSQL:

UPDATE Meter
SET Name = CONCAT('ZAA' ,'_', SUBSTRING(Name, 4, LENGTH(Name)))
WHERE Name LIKE 'ZAA\%'
于 2015-03-31T05:16:44.143 に答える