Analytics
Enthusiast

## Difference between Timestamp

Hi,

Can some one help me how to calculate the Difference of Timestamp fields/columns?

Gopi
8 REPLIES
Enthusiast

## Re: Difference between Timestamp

Gopi,

It should simply be the arithmetic difference, ie timestamp2 - timestamp1.

What problem are you having?

The following is taken straight out of the Teradata Functions and Operators reference manual.

Example 1: Subtract two TIMESTAMP Types

Consider the following table:
CREATE TABLE BillDateTime
(start_time TIMESTAMP(0)
,end_time TIMESTAMP(0));

Determine the difference, specifying an Interval unit of DAY TO SECOND for the result:
SELECT (end_time - start_time) DAY(4) TO SECOND FROM BillDateTime;

The DAY(4) specifies four digits of precision, and allows for a maximum of 9999 days, or
approximately 27 years.

The result is an Interval that looks like this:
5 16:49:20.340000

(Note the format specification for the result.)

Peter
Enthusiast

## Re: Difference between Timestamp

I tried like field1 - field2 without DAY(n) command and it throwed the error. I got it now.
Enthusiast

## Re: Difference between Timestamp

What can you do if there are more than 9999 days between the timestamps?  I am trying to get the number of seconds elapsed (ideally as decimal rather than interval).  For instance, this gives an error:

`SELECT`

`CURRENT_TIMESTAMP AS t2,`

`(t2 - INTERVAL '9999' DAY - INTERVAL '30' DAY) t1,`

`(t2 - t1) DAY(4) TO SECOND delta`

I can write a case statement that returns results only when the difference is between +/-9999 days, but when the difference is outside this range, it can only return `null`.  I would like to get the number of seconds for these cases.
Enthusiast

## Re: Difference between Timestamp

From another thread and some playing around, I found that this works:

`SELECT CURRENT_TIMESTAMP AS t2, (t2 - INTERVAL '9999' DAY - INTERVAL '30' DAY + INTERVAL '1.3' SECOND) AS t1, CAST((CAST(t2 AS DATE) - DATE '1970-01-01') * 86400 + (EXTRACT(HOUR FROM t2) * 3600) + (EXTRACT(MINUTE FROM t2) * 60) AS DECIMAL(18)) + (EXTRACT(SECOND FROM t2)) AS t2_s, CAST((CAST(t1 AS DATE) - DATE '1970-01-01') * 86400 + (EXTRACT(HOUR FROM t1) * 3600) + (EXTRACT(MINUTE FROM t1) * 60) AS DECIMAL(18)) + (EXTRACT(SECOND FROM t1)) AS t1_s, t2_s - t1_s AS delta_s`

Not applicable

## Re: Difference between Timestamp

hi,

i need to know more about the below code

seconds := (3600 * (To_Number(extract(HOUR FROM diff)))) + (60 * (To_Number(extract (MINUTE FROM diff)))) +(To_Number(extract(SECOND FROM diff)));

I guess TO_NUMBER() does not work in teradata

So how this part of code can be convert into Teradata.

Junior Contributor

## Re: Difference between Timestamp

Conversion is easy, just remove the TO_NUMBER :-)

Or use TD14, which supports it.

There was a thread last week on timestamp difference in seconds/minutes:

Dieter

Enthusiast

## Re: Difference between Timestamp

Hi,

Did you get any solution to find the timestamp difference in seconds.

Im looking for some solution for the below.

GopiKrishnan_S wrote:
Hi,

Can some one help me how to calculate the Difference of Timestamp fields/columns?

Gopi

ex: t1 = 4/15/2017 17:34:52

t2 = 4/16/2017 18:43:54

t2 - t1 = 86400+3600+540+ 2 =3686942 seconds

Enthusiast

## Re: Difference between Timestamp

Can anyone provide me solution for timestamp difference in seconds.

t1 = 4/15/2017 17:34:52

t2 = 4/16/2017 18:43:54

t2 - t1 = 86400+3600+540+ 2 =90542 seconds

i know that if we subtract timestamps we will get

('4/17/2017 10:43:17'- '4/15/2017 17:34:52') DAY(4) TO SECOND as diff the answer is    1 17:08:25.000000. But im looking for some solution that give me caluculated value in Seconds.