|
Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - DATEADD() Function Usage Examples
By: FYIcenter.com
(Continued from previous topic...)
How To Use DATEADD() Function?
DATEADD() is a very useful function for manipulating date and time values.
The following tutorial exercise shows you some good DATEADD() usage examples:
-- Incrementing 1 year on a leap date
DECLARE @birth_date DATETIME;
SET @birth_date = '2000-02-29 16:00:00.000';
SET @birth_date = DATEADD(year, 1, @birth_date);
SELECT @birth_date;
GO
2001-02-28 16:00:00.000
-- Decrementing 1 month on a leap date
DECLARE @birth_date DATETIME;
SET @birth_date = '2000-02-29 16:00:00.000';
SET @birth_date = DATEADD(month, -1, @birth_date);
SELECT @birth_date;
GO
2000-01-29 16:00:00.000
-- Incrementing 2 milliseconds
DECLARE @birth_date DATETIME;
SET @birth_date = '2000-02-29 16:00:00.000';
SET @birth_date = DATEADD(millisecond, 2, @birth_date);
SELECT @birth_date;
GO
2000-02-29 16:00:00.003
(Continued on next topic...)
- How To Add or Remove Days on Date and Time Values?
- Can Date and Time Values Be Converted into Integers?
- Can Integers Be Converted into Date and Time Values?
- Are DATETIME and NUMERIC Values Convertible?
- Can a DATETIME Value Be Subtracted from Another DATETIME Value?
- What Are the Date and Time Functions Supported by SQL Server 2005?
- How To Increment or Decrement Parts of DATETIME Values?
- How To Use DATEADD() Function?
- How To Calculate DATETIME Value Differences Using the DATEDIFF() Function?
- How To Calculate Age in Days, Hours and Minutes?
- How To Get Month and Weekday Names from DATETIME Values?
- How To Get Parts of DATETIME Values as Integers?
- How To Get Year, Month and Day Out of DATETIME Values?
- What Is the Difference Between GETDATE() and GETUTCDATE()?
- How To Format Time Zone in +/-hh:mm Format?
- How To Format DATETIME Values to Strings with the CONVERT() Function?
- How To Truncate DATETIME Values to Dates without Time?
- How To Set Different Parts of a DATETIME Value?
|