Send feedback on this topic.
Teradata.Client.Provider
Arithmetic Operations
.NET Data Provider for Teradata > Developer's Guide > Data Types > Date And Time Data Types > Arithmetic Operations

Each of the Date And Time Types support the addition and subtraction of a time span. The time span is represented by System.TimeSpan.

Subtraction between the same type is also supported. This results is the difference in time (time span). The object that is returned from this operation is also a System.TimeSpan.

An example of these operations is as follows:

Subtraction Example
Copy Code
System.TimeSpan daysToAdd = new TimeSpan(101, 0, 0, 0); //101 days
TdDate christmas = new TdDate(2007, 12, 25);
TdDate newDate = christmas + daysToAdd;

//The output will be:
//    The date 101 days after Christmas will be: 2008-04-04
Console.WriteLine("The date 101 days after Christmas will be: {0}", newDate);

//Going to calculate the number of days from Valentine's day to the following New Year's Day
TdDate valentine = new TdDate(2008, 2, 14);
TdDate newYear = new TdDate(2009, 1, 1);
TimeSpan daysToNewYear = newYear - valentine;

//The output will be:
//    There are 335 days from 2008-02-14 to 2009-01-01.
Console.WriteLine("The date {0} days from {1} to {2}",
    daysToNewYear.Days, valentine.ToString(), newYear.ToString());

TdDate Arithmetic Operations

TdDate supports addition and subtraction of the Day-Time Interval Provider Specific types and the Year-Month Interval Provider Specific types. The Day-Time Interval types include:

The Year-Month Interval types include:

The TdDate type operators define arithmetic support for the Interval TdIntervalDayToSecond and the Interval TdIntervalYearToMonth. But implicit conversion of the other Day-Time Interval types to TdIntervalDayToSecond and the other Year-Month Interval types to TdIntervalYearToMonth provides support for these other types. For a list of the Interval implicit conversion type support, please see Interval Implicit Conversions.

When an arithmetic operation is performed between a TdDate and a System.TimeSpan (or any Day-Time Interval), only the day component of the type is considered in the operation. The time components are ignored. The following are examples:

TdDate Arith. Op TimeSpan/Interval TdDate Result
"2008-10-10" + "0.20:32:43" "2008-10-10"
"2010-12-15" + "4.05:43:00" "2010-12-19"
"2009-01-04" - "1.25:10:10" "2009-01-02"
"2009-02-14" - Interval '100 20:10:30.000002" Day To Second "2008-11-06"
"1999-11-04" + Interval '24' Month "2001-11-04"

TdTime(WithTimeZone) Arithmetic Operations

TdTime and TdTimeWithTimeZone supports addition and subtraction of only the Day-Time Interval Provider Specific types. Any day components of the Day-Time Interval types will be ignored in these operations. These Day-Time Interval types include:

The TdTime(WithTimeZone) type operators define arithmetic support for the Interval TdIntervalDayToSecond. But implicit conversion of the other Day-Time Interval types to TdIntervalDayToSecond provides support for these other types. For a list of the Interval implicit conversion type support, please see Interval Implicit Conversions.

When an arithmetic operation is performed between a TdTime(WithTimeZone) and a System.TimeSpan or a Day-Time Interval, only the time components of the type are considered in the operation. The day component is ignored.

The TdTime(WithTimeZone) provider type supports a scale of up to 6 and the TimeSpan type can support a scale of up to 7. If a TdTime(WithTimeZone) and a TimeSpan (or a Day-Time Interval) have different scales, the arithmetic operation is performed using the scales of both operands. The result is then truncated to the scale of TdTime(WithTimeZone) operand.

In addition, if the TimeSpan operand has a scale of 7, the 7th digit (least significant) is ignored by the arithmetic operations.

The following are examples:

TdTime(WithTimeZone) Arith. Op TimeSpan/Interval TdTime(WithTimeZone) Result
"12:10:43.221" - "1.00:10:00.000912" "12:10:53.220"
"23:10:00" + "10.02:00:00.9" "01:10:00"
"04:00:00+08:00" - "20.6:00:00.1" "21:59:59+08:00"
"10:20:00.212939" - "0.00:00:00.0000009" "10:20:00.212939"
"04:20:34.909+04:00" + Interval '100 20:10:30.500002' Day To Second "00:31:05.409+04:00"
"10:20:00.212939" - Interval '2000' Day "10:20:00.212939"

TdTimestamp(WithTimeZone) Arithmetic Operation

The behavior of the arithmetic operations between a TdTimestamp(WithTimeZone) and TimeSpan is the same as that of TdTime(WithTimeZone) Arithmetic Operations.

TdTimestamp and TdTimestampWithTimeZone supports addition and subtraction of the Day-Time Interval Provider Specific types and the Year-Month Interval Provider Specific types. A list of these Interval types may be referenced in TdDate Arithmetic Operations.

The following are examples:

TdTimestamp(WithTimeZone) Arith. Op TimeSpan/Interval TdTimestamp(WithTimeZone) Result
"2008-10-12 00:00:10" + "10.12:10:05.98" "2008-10-22 12:10:15"
"2010-01-21 12:00:00.121982" - "1.00:00:00.0000009" "2010-01-20 12:00:00.121982"
"2008-09-15 21:00:00.3294+08:00" - "2.01:10:00.231" "2008-09-17 22:10:00.5604+08:00"
"2008-09-15 21:00:00.3294+08:00" - Interval '100 20:10:30.000002' Day To Second "2008-06-06 03:49:30.3293+08:00"
"2009-03-20 12:20:30+04:00" + Interval '300-09' Year To Month "2309-12-20 12:20:30+04:00"