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
51
votes
8 answers

How to split a string after specific character in SQL Server and update this value to specific column

I have table with data 1/1 to 1/20 in one column. I want the value 1 to 20 i.e value after '/'(front slash) is updated into other column in same table in SQL Server. Example: Column has value 1/1,1/2,1/3...1/20 new Column value 1,2,3,..20 That is, I…
SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
51
votes
5 answers

Update values incrementally in mysql

One field of my table's field is set to 0 for all rows. But I want to update the incremental value by step 1 in an update query. How can I do that in mysql?
hd.
  • 17,596
  • 46
  • 115
  • 165
51
votes
10 answers

MySQL - How do I enter NULL?

In SQL, I know I can do: UPDATE TableA SET MyColumn=NULL But, how do I enter NULL into a cell graphically? I tried entering 'null' and 'NULL' and '' (nothing/empty string) into the MySQL Query Browser with no success.
rlb.usa
  • 14,942
  • 16
  • 80
  • 128
51
votes
4 answers

When running UPDATE ... datetime = NOW(); will all rows updated have the same date/time?

When you run something similar to: UPDATE table SET datetime = NOW(); on a table with 1 000 000 000 records and the query takes 10 seconds to run, will all the rows have the exact same time (minutes and seconds) or will they have different times?…
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
48
votes
10 answers

Update values from one column in same table to another in SQL Server

I'm trying to overwrite values that are found in TYPE1 with values that are found in TYPE2. I wrote this SQL to try it out, but for some reason it isn't updating: select * from stuff update stuff set TYPE1 = TYPE2 where TYPE1 is null; update…
neuquen
  • 3,991
  • 15
  • 58
  • 78
47
votes
3 answers

MySQL: Creating a new table with information from a query

In MySQL, I would like to create a new table with all the information in this query: select * into consultaa2 from SELECT CONCAT( 'UPDATE customers SET customers_default_address_id= ', (SELECT a.address_book_id FROM address_book a where …
Saikios
  • 3,623
  • 7
  • 37
  • 51
47
votes
2 answers

Update with result from CTE

I want to update job date if any records is an earlier date. Trying to use CTE to achieve this: CREATE TABLE job (jobid int4, jobdate date); INSERT INTO job (jobid, jobdate) VALUES (1, '2016-02-01'), (2, '2016-02-01'), (3,…
sibert
  • 1,968
  • 8
  • 33
  • 57
47
votes
6 answers

Django update queryset with annotation

I want to update all rows in queryset by using annotated value. I have a simple models: class Relation(models.Model): rating = models.IntegerField(default=0) class SignRelation(models.Model): relation = models.ForeignKey(Relation,…
ramusus
  • 7,789
  • 5
  • 38
  • 45
46
votes
4 answers

Within a trigger function, how to get which fields are being updated

Is this possible? I'm interested in finding out which columns were specified in the UPDATE request regardless of the fact that the new value that is being sent may or may not be what is stored in the database already. The reason I want to do this is…
EvilAmarant7x
  • 2,059
  • 4
  • 24
  • 33
46
votes
3 answers

SQL UPDATE order of evaluation

What is the order of evaluation in the following query: UPDATE tbl SET q = q + 1, p = q; That is, will "tbl"."p" be set to q or q + 1? Is order of evaluation here governed by SQL standard? Thanks. UPDATE After considering Migs' answer, I ran some…
pilcrow
  • 56,591
  • 13
  • 94
  • 135
45
votes
2 answers

How to update MySql timestamp column to current timestamp on PHP?

I want to update the columns of data type timestamp manually through my PHP code. Can you please tell me how to do that?
Abhi
  • 5,501
  • 17
  • 78
  • 133
45
votes
9 answers

How to UPDATE all columns of a record without having to list every column

I'm trying to figure out a way to update a record without having to list every column name that needs to be updated. For instance, it would be nice if I could use something similar to the following: // the parts inside braces are what I am trying to…
BinaryCat
  • 1,220
  • 2
  • 17
  • 32
44
votes
6 answers

SQL Update Multiple Fields FROM via a SELECT Statement

This works, but i would like to remove the redundancy. Is there a way to merge the update with a single select statement so i don't have to use vars? DECLARE @OrgAddress1 varchar, @OrgAddress2 varchar, @OrgCity varchar, …
KellCOMnet
  • 1,859
  • 3
  • 16
  • 27
43
votes
5 answers

Update mysql table with data from another table

Is it possible to run an UPDATE command on mysql 5.0 with a sub select. The command I would like to run is this: UPDATE book_details SET live = 1 WHERE ISBN13 = '(SELECT ISBN13 FROM book_details_old WHERE live = 1)'; ISBN13 is currently stored as…
William Macdonald
  • 2,139
  • 2
  • 21
  • 27
43
votes
4 answers

Update statement to update multiple rows

I have a question regarding the following syntax. Is there a cleaner way to roll this up into one statement rather than two. I've tried several iterations but this seems to be the only way I can successfully execute these two statements. UPDATE…
user2454335
  • 431
  • 1
  • 4
  • 3