|
Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - Subtracting a DATETIME Value from Another DATETIME Value
By: FYIcenter.com
(Continued from previous topic...)
Can a DATETIME Value Be Subtracted from Another DATETIME Value?
Can a datetime value be subtracted from another datetime value? The answer is yes.
The subtract operation can be performed by the subtract operator (-) as:
- datetime1 - datetime2: Returning a DATETIME value calculated as
CONVERT(DATETIME, CONVERT(NUMERIC(18,9),datetime1) - CONVERT(NUMERIC(18,9),datetime2)).
The tutorial exercise below shows you some good examples:
DECLARE @birth_date DATETIME;
DECLARE @today_date DATETIME;
SET @birth_date = '2000-02-29 16:00:00.000';
SET @today_date = '2007-05-19 22:00:00.000';
SELECT @today_date - @birth_date;
GO
1907-03-22 06:00:00.000
DECLARE @birth_date DATETIME;
DECLARE @today_date DATETIME;
DECLARE @age NUMERIC(9,2);
SET @birth_date = '2000-02-29 16:00:00.000';
SET @today_date = '2007-05-19 22:00:00.000';
SET @age = CONVERT(NUMERIC(9,2), @today_date - @birth_date);
SELECT @age;
GO
2636.25
(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?
|