使用空格更新查询

时间:2014-09-18 05:56:46

标签: sql postgresql

我有这样的更新查询:

update table 
set col = 'hello hi' 
where col = 'hi hello'

但它没有更新值为'hi hello'的列(hihello之间的多个空格)。有什么建议吗?

3 个答案:

答案 0 :(得分:1)

如果需要使用不同数量的空格更新每一行,则需要使用此命令:

update table 
set col = 'hello hi' 
where col LIKE 'hi %hello'

%是“任何字符,任意数量的重复”的占位符

答案 1 :(得分:1)

在Postgres中,您可以使用正则表达式:

update the_table
set col = 'hello hi'
where col ~ '^(hello)\s+(hi)$';

需要开头的^和结尾的$,以避免更新列中包含的行。 'here hello hi''hello hi there'

如果在hello之前或hi之后可以有空格,那么您可以添加其他通配符:

update the_table
set col = 'hello hi'
where col ~ '^\s*(hello)\s+(hi)\s*$';

另一种选择是使用第一个正则表达式对列本身使用trim():

update the_table
set col = 'hello hi'
where trim(col) ~ '^(hello)\s+(hi)$';

有关正则表达式的更多详细信息,请参阅手册:http://www.postgresql.org/docs/current/static/functions-matching.html

答案 2 :(得分:0)

避免使用通配符的问题

update table 
set col = 'hello hi' 
where LEFT(col, 3) = 'hi ' 
      AND
      RIGHT(col, 6) = ' hello'
      AND
      SUBSTRING(col, 3, LEN(col)-7) = REPLICATE(' ',  LEN(col)-7)
相关问题