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
27
votes
3 answers

SQL Stored Procedure: If variable is not null, update statement

I have an update statement in a stored procedure that looks generally like this: Update [TABLE_NAME] Set XYZ=@ABC Is there a good way to only trigger the update statement if the variable is not null or the value -1? Similar to an IF NOT…
Jake
  • 273
  • 1
  • 3
  • 4
27
votes
3 answers

Android Room persistence library @Update not working

I am trying to update my database via new android room library, but it is not working. Here it is my approach @IgnoreExtraProperties @Entity(tableName = CarModel.TABLE_NAME, indices = {@Index(value = "car_name", unique = true)}) public class…
nAkhmedov
  • 3,522
  • 4
  • 37
  • 72
27
votes
2 answers

How to add a column to a table from another table in Mysql?

I have two tables table1 table2 Tabel1 contains 2 columns id Name Tabel2 contains 2 columns id Age A want to add age column from table2 to table1 (WHERE table1.id = table2.id) Then table1 should contains 3 columns id Name Age
Codesl
  • 547
  • 2
  • 5
  • 10
26
votes
4 answers

NULL value for int in Update statement

Is it possible to set NULL value for int column in update statement? How can I write the update statement in this case?
Maanu
  • 5,093
  • 11
  • 59
  • 82
26
votes
1 answer

Knex.js - How To Update a Field With An Expression

How do we get Knex to create the following SQL statement: UPDATE item SET qtyonhand = qtyonhand + 1 WHERE rowid = 8 We're currently using the following code: knex('item') .transacting(trx) .update({qtyonhand: 10}) .where('rowid',…
A2MetalCore
  • 1,621
  • 4
  • 25
  • 49
26
votes
7 answers

How do I change all empty strings to NULL in a table?

I have a legacy table with about 100 columns (90% nullable). In those 90 columns I want to remove all empty strings and set them to null. I know I can: update table set column = NULL where column = ''; update table set column2 = NULL where column2 =…
Kyle West
  • 8,934
  • 13
  • 65
  • 97
26
votes
5 answers

MySQL - How do I update the decimal column to allow more digits?

I'm a beginner in MySQL, and I accidentally created a table with a column named (price decimal(2,2)); It needs to be decimal(4,2) to allow 4 digits. Since I've already created it, what is the easiest way to update that decimal value to…
ValleyDigital
  • 1,460
  • 4
  • 21
  • 37
26
votes
3 answers

Is there any way to rollback after commit in MySQL?

I did a big mistake that I updated a table without 'where' clause in MySQL :'( It is auto-committed. Is there any way to rollback from it?
Johnny Lim
  • 5,623
  • 8
  • 38
  • 53
25
votes
6 answers

How to update multiple columns in single update statement in DB2

I want to update multiple columns of a table in DB2 with single Update statement. Any hint or idea will be appreciable. Thanks.
Superman
  • 871
  • 2
  • 13
  • 31
25
votes
6 answers

SQL update undo

Is there a way we can undo a SQL update query?
Balaji
  • 2,109
  • 5
  • 27
  • 34
25
votes
2 answers

Mysql: Update field of most latest record

I'm trying to update the latest record where name is John (John has multiple records but different ID) but I seem to be in a bind. What's wrong with my query? UPDATE messages_tbl SET is_unread=1 WHERE ReceiveTime = (SELECT MAX(ReceiveTime) FROM…
enchance
  • 29,075
  • 35
  • 87
  • 127
25
votes
1 answer

Update multiple rows with multiple 'where' clauses for each individual row

I am trying to update my table like this: Update MyTable SET value = 1 WHERE game_id = 1, x =-4, y = 8 SET value = 2 WHERE game_id = 1, x =-3, y = 7 SET value = 3 WHERE game_id = 2, …
Dex
  • 704
  • 2
  • 9
  • 22
25
votes
7 answers

Round up value to nearest whole number in SQL UPDATE

I'm running SQL that needs rounding up the value to the nearest whole number. What I need is 45.01 rounds up to 46. Also 45.49 rounds to 46. And 45.99 rounds up to 46, too. I want everything up one whole digit. How do I achieve this in an UPDATE…
Skuta
  • 5,830
  • 27
  • 60
  • 68
24
votes
4 answers

MySQL/SQL: Update with correlated subquery from the updated table itself

I have a generic question that I will try to explain using an example. Say I have a table with the fields: "id", "name", "category", "appearances" and "ratio" The idea is that I have several items, each related to a single category and "appears"…
Roee Adler
  • 33,434
  • 32
  • 105
  • 133
24
votes
8 answers

The best way to remove value from SET field?

Which is the best way to update a mysql SET field, to remove a specific value from the field. Eg. field categories with values: 1,2,3,4,5? I want to remove '2' from the list: UPDATE table SET categories = REPLACE(categories, ',2,', ',') WHERE…
morandi3
  • 1,095
  • 3
  • 14
  • 27