Update query with spaces

user3492455

I have an update query like this:

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

But it is not updating the column which has the value as 'hi hello' (more than one space in between hi and hello). Any suggestions?

a_horse_with_no_name

In Postgres you can use a regular expression for that:

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

The ^ at the beginning and the $ at the end are needed to avoid updating a row where the column contains e.g. 'here hello hi' or 'hello hi there'

If there can be spaces before hello or after the hi then you can add additional wildcards:

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

Another option would be to use trim() on the column itself with the first regular expression:

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

More details about regular expressions are in the manual: http://www.postgresql.org/docs/current/static/functions-matching.html

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

if query is not update

分類Dev

SQL Query that returns n spaces

分類Dev

Update query if statement for Oracle

分類Dev

Error in Mysql UPDATE query

分類Dev

Update query string in url

分類Dev

php UPDATE not working query

分類Dev

Oracle Query Update From

分類Dev

UPDATE, INSERT and ON DUPLICATE KEY UPDATE in one query

分類Dev

how to use update query in mongoDb?

分類Dev

how to use update query in mongoDb?

分類Dev

mysql query to update duplicate entries

分類Dev

SQL Query Update using a join

分類Dev

update query throws exception in python

分類Dev

Yii update query not working as expected

分類Dev

PHP - update query using postman return success but not update the value in database

分類Dev

Why this Join-Update query update all value?

分類Dev

How to create MongoDB Update query to update certain elements in an array of objects

分類Dev

SQLAlchemy: Using delete/update with a join query

分類Dev

MS Access Update Query (Multiple Where Conditions)

分類Dev

How to execute batch SQL update query in Clojure?

分類Dev

Won't Query on the UPDATE part of the program

分類Dev

MySQL query works for SELECT but not with UPDATE statement

分類Dev

Optimizing a Sql Server Query to update large list

分類Dev

OOP For Loop SQL Update Query Quantity

分類Dev

how to pass an array into update query kdb

分類Dev

run a bulk update query in cassandra on 1 column

分類Dev

room db update multiple rows with @query

分類Dev

How I query the update mutation at graphql?

分類Dev

Count in update query doesn't work in Laravel