SQL UPDATE
The UPDATE command is used to update existing rows in a table.Which rows is to be update, it is decided by a condition. To specify condition, we use WHERE clause.
Let’s see the Syntax:
UPDATE table_name
SET column_name = expression
WHERE conditions
Let’s take an example: here we are going to update an entry in the Student table.
SQL statement:
UPDATE Student
SET Gender = 'Male'
WHERE Student_Name = 'Dinkar Mishra'
Student Table:
Student_Name | Gender | Mobile_Number | HOME_TOWN |
Ramesh Patel | Male | 7503896532 | Lucknow |
Dinkar Mishra | Female | 9270568893 | Varanasi |
Soham SHARMA | Male | 9990449935 | Lucknow |
See the result after updating value:
Student_Name | Gender | Mobile_Number | HOME_TOWN |
Ramesh Patel | Male | 7503896532 | Lucknow |
Dinkar Mishra | Male | 9270568893 | Varanasi |
Soham SHARMA | Male | 9990449935 | Lucknow |
Updating Multiple Fields:
If you are going to update multiple fields, you should separate each field assignment with a comma.
SQL UPDATE statement for multiple fields:
UPDATE Student
SET Gender = 'Female' HOME_TOWN = ‘Pune’
WHERE Student_Name = 'Dinkar Mishra'
Result of the table is given below:
Student_Name | Gender | Mobile_Number | HOME_TOWN |
Ramesh Patel | Male | 7503896532 | Lucknow |
Dinkar Mishra | Female | 9270568893 | Pune |
Soham SHARMA | Male | 9990449935 | Lucknow |
SQL UPDATE DATE
To update a date & time field in SQL, you should use the following query.
Syntax of sql update date.
UPDATE table
SET Column_Name = 'YYYY-MM-DD HH:MM:SS'
WHERE Id = value
For example:
If you want to change the first row which id is 1 then you should write the following syntax:
UPDATE table
SET EndDate = '2014-03-16 00:00:00.000'
WHERE Id = 1
Note: you should always remember that SQL must attach default 00:00:00.000 automatically.
This query will change the date and time field of the first row in that above assumed table.