使用Update语句替换空格

时间:2017-03-28 19:51:24

标签: sql sql-server sql-update

我使用下面的SQL更新语句将所有空值替换为“不需要签名”,但是当我运行它时,我的空白量与我开始时相同。我做错了什么?

BEGIN TRANSACTION; 
UPDATE 
    shipments
SET 
    signature_required = REPLACE(signature_required,'','Signature not required')
WHERE 
    signature_required = ''
Commit Transaction;

3 个答案:

答案 0 :(得分:3)

删除'替换&#39>简化set语句。功能。使用WHERE子句定位空白值。

UPDATE 
shipments
SET 
signature_required = 'Signature not required'
WHERE 
signature_required = ''

仅当值实际存储为空字符串时才会起作用。

答案 1 :(得分:0)

你必须放一个空格

BEGIN TRANSACTION; 
UPDATE 
    shipments
SET 
    signature_required = REPLACE(signature_required,' ','Signature not required')
WHERE 
    signature_required = ' '
Commit Transaction;

BEGIN TRANSACTION; 
UPDATE 
    shipments
SET 
    signature_required = REPLACE(isnull(Signature_required,' '),' ','Signature not required')
WHERE 
    ISNULL(signature_required,' ') = ' '
Commit Transaction;

答案 2 :(得分:0)

如果'blank'表示空字符串或null,则使用coalesce()返回空字符串进行比较将起作用:

update shipments
set signature_required = 'signature not required'
where coalesce(signature_required,'') = '';

或使用or

update shipments
set signature_required = 'signature not required'
where signature_required = ''
   or signature_required is null