Update date field in SQL Server
To update a date field with T-SQL, here is the general syntax:
UPDATE table_name
SET date_field = 'date_value'
[WHERE conditions];
To update with the current date:
UPDATE
table_name
SET
date_field = getdate();
To update with the current date and time:
UPDATE
table_name
SET
date_field = CURRENT_TIMESTAMP;
To update with a specific date value:
UPDATE
table_name
SET
date_field = 'YYYY-MM-DD HH:MM:SS.MMM';
When you update with a specific date value and not sure about the date format, use CAST
:
UPDATE
table_name
SET
date_field = CAST('date_value' AS DATETIME);
For example:
UPDATE
student
SET
registration_date = CAST('2019-09-25' AS DATETIME)
WHERE
id = 25;
Need a good GUI tool for databases? TablePlus provides a native client that allows you to access and manage Oracle, MySQL, SQL Server, PostgreSQL, and many other databases simultaneously using an intuitive and powerful graphical interface.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS