Questions tagged [sql-update]

An SQL UPDATE statement is used to change existing rows in a table.

An SQL UPDATE statement is used to change existing rows in a table.

The basic syntax is:

UPDATE table 
SET 
    Column1 = 'x',
    Column2 = 'y'
WHERE id=1

Tagging Recommendation

This tag should be used for general SQL programming language questions, in addition to tags for specific products. For example, questions about Microsoft SQL Server should use the tag, while questions regarding MySQL should use the tag. Tagging by product (including version, e.g , ) is the easiest way to know what functionality is available for the task at hand.

References

12009 questions
20
votes
2 answers

android.database.sqlite.SQLiteDatabase.rawQuery() is not updating a DATETIME column with a SQLite datetime() function

public Cursor set_datetime_next(Reminder r) { String _newVal = "datetime('now', '+7 days')"; String[] args = { new Integer(r.getID()).toString() }; String query = "UPDATE " + DBConst.TABLE + " SET " +…
JD.
  • 3,005
  • 2
  • 26
  • 37
20
votes
2 answers

How to update a field based on its current value in MySQL?

Is it possible to get the current value of a field, use it as a variable in a calculation, then update the field based on the result? For example the record with the ID "1" in table1 has a value of "2" SELECT table1 WHERE ID = "1" SET RESULT to…
blunders
  • 3,619
  • 10
  • 43
  • 65
20
votes
3 answers

SQLAlchemy update multiple rows in one transaction

How can I update multiple, existing rows in a database, using dictionary that maps existing values for one column, to the required new values for another column? I have a table: class MyTable(BaseModel): col1 = sa.Column(sa.String(256)) col2…
sken3r
  • 414
  • 1
  • 4
  • 10
20
votes
2 answers

Update with join with BigQuery

Is it possible to do an UPDATE on a table based on a JOIN with an existing table in BigQuery? When I try this statement on the following database (https://bigquery.cloud.google.com/dataset/pfamdb:pfam31), UPDATE pfam31.uniprot SET…
saladi
  • 3,103
  • 6
  • 36
  • 61
20
votes
2 answers

SQL update one column from another column in another table

I read various post's prior to this. but none of them seemed to work for me. As the title suggests, I am trying to update one column from a column in another table. I don't recall having problems with this before.. 1. Table:…
Kenny Cason
  • 12,109
  • 11
  • 47
  • 72
20
votes
7 answers

update all NULL fields MySQL

I'd like to update all NULL fields in one table to 0. Of course UPDATE mytable SET firstcol=0 WHERE firstcol IS NULL would do the job. But I wonder if there´s a smarter solution than just c&p this line for every column.
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
20
votes
2 answers

Updating multiple rows with different primary key in one query in PostgreSQL?

I have to update many columns in many rows in PostgreSQL 9.1. I'm currently doing it with many different UPDATE queries, each one that works on a different row (based on the primary key): UPDATE mytable SET column_a = 12, column_b = 6 WHERE id =…
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
20
votes
3 answers

Update a boolean to its opposite in SQL without using a SELECT

Is it possible to upgrade a bool field by telling it to update the field to the opposite of what it is without having to select the value - check it then update accordingly which seems long winded... A pseudo example of what i mean UPDATE `table`…
Sir
  • 8,135
  • 17
  • 83
  • 146
20
votes
8 answers

SQL Server Update Trigger, Get Only modified fields

I am aware of COLUMNS_UPDATED, well I need some quick shortcut (if anyone has made, I am already making one, but if anyone can save my time, I will appriciate it) I need basicaly an XML of only updated column values, I need this for replication…
Akash Kava
  • 39,066
  • 20
  • 121
  • 167
20
votes
7 answers

How to recreate the InitCap() function in T-SQL to apply first-letter capitalization to a string?

I have a table on my database. My table's name is "Company". I want to change data "company_name" as upper case first letter. For example; "ABC COMPANY" "DEF PLASTICITY" as "Abc Company" "Def Plasticity" I know that I should use "UPDATE" command.…
cethint
  • 2,231
  • 8
  • 28
  • 31
19
votes
2 answers

MySQL: Update a full table, inserting a MD5 hash, for each row a specific one

I added a column to an existing table. Now I need to update the tablecontent by adding a MD5 hash to that new column, based on the content of an existing column. To be more precise: id | name | date-of-birth | hash 1 | test | 12.12.12 |…
Ralf Marmorglatt
  • 265
  • 1
  • 2
  • 10
19
votes
2 answers

SQL Update one table based on conditions in another table

Two tables. Content (table), topic_id (primary key), data (text) Topics (table), topic_id (primary key), content_type (text) Both tables have the same primary key data (topic_id). I need to update the data field (Content table) with…
Josh Bond
  • 1,719
  • 4
  • 17
  • 26
19
votes
7 answers

SQL UPDATE, but only if the old value is null

I have been using a sql like this to update a list of properties in my database: update my_table set a = ?, b = ?, c = ?, d = ?, where customer = ? But I want to update a property with a new value ONLY if it does not have a value in the database…
Miss T
  • 191
  • 1
  • 1
  • 3
19
votes
1 answer

How to conditionally INSERT OR REPLACE a row in SQLite?

I would like to insert or replace_on_condition. If the condition is not satisfied, do not insert or replace. Is this possible? For my project, I currently have two data collection processes. One is fast but doesn't catch everything. The other is…
sapbucket
  • 6,795
  • 15
  • 57
  • 94
19
votes
1 answer

Postgres RETURNING clause with join

In the following SQL, how could I make the RETURNING clause join to something else and return the joined row(s)? Here it only returns the row from mytable that was updated, but I'd like it to return that row joined to something in another…
user779159
  • 9,034
  • 14
  • 59
  • 89