Solved

SQL modify date by two days using update query

Posted on 2008-09-30
5
639 Views
Last Modified: 2012-05-05
Is it possible to use an update query to change a time value on every row by 2 days, say using the dateadd function?
0
Comment
Question by:pansophy
  • 2
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 22608192
update t
set datefield = dateadd(d, 2, datefield)
from tablename t
0
 
LVL 9

Expert Comment

by:jamesgu
ID: 22608438
another easy ways is:

update table_name
set column_name = column_name - 2 /* or + 2 */

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22608455
assuming that your system is setup to subtract days rather than hours or months, etc.
0
 

Author Closing Comment

by:pansophy
ID: 31501667
thanks!
0
 
LVL 9

Expert Comment

by:jamesgu
ID: 22608595
http://msdn.microsoft.com/en-us/library/aa276859(SQL.80).aspx



B. Use the addition operator to add days to date and time values
 

This example adds a number of days to a datetime date.
 

USE master

GO

SET NOCOUNT ON

DECLARE @startdate datetime, @adddays int

SET @startdate = '1/10/1900 12:00 AM'

SET @adddays = 5

SET NOCOUNT OFF

SELECT @startdate + 1.25 AS 'Start Date', 

   @startdate + @adddays AS 'Add Date'
 

Here is the result set:
 

Start Date                  Add Date                    

--------------------------- --------------------------- 

Jan 11 1900  6:00AM         Jan 15 1900 12:00AM         
 

(1 row(s) affected)

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now