Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Update Date Field in SQL Server

  • Difficulty Level : Expert
  • Last Updated : 10 Oct, 2021

The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statements as per our requirement. With this article, we will learn how to Update the Date Field in SQL Server. In this article, we will be making use of the Microsoft SQL Server as our database.

So, we will create a database first:

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

Step 1: Create Database

Query:



CREATE DATABASE GFG

Step 2: Use Database

Query:

USE GFG

Step 3: Create a table

Create a table (TimeTable) in the database to store the data.

Query:

CREATE TABLE TimeTable(
 SubjectDate datetime NOT NULL ,
 SubjectName char (10)
)

Step 4: Insert the data into the database 

Query:

/* Data Inserted for a full week dates*/

INSERT INTO TimeTable (SubjectDate , SubjectName) VALUES ('01/10/2021','DSA')
INSERT INTO TimeTable (SubjectDate , SubjectName) VALUES ('02/10/2021','CD')
INSERT INTO TimeTable (SubjectDate , SubjectName) VALUES ('03/10/2021','DBMS')
INSERT INTO TimeTable (SubjectDate , SubjectName) VALUES ('04/10/2021','OS')

Step 5: Now change the data of DateTime

In the below query, the SET statement is used to set new values to the particular column, and the WHERE clause is used to select the rows for which the columns are needed to be updated. If we have not used the WHERE clause then the columns in all the rows will be updated. So the WHERE clause is used to choose the particular rows.

Query:

UPDATE TimeTable   
SET SubjectDate = '05/10/2021'   
WHERE SubjectName = 'DSA'   

Output:

As we can see subject date has been changed.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!