If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Convert timestamp to integer

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-21-10, 17:39
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Convert timestamp to integer

Hi,

How can I convert the current timestamp ( or any other date of the format 2010-05-21-00.00.00.000000) to an integer. For example, I want to convert 2010-05-21-05.32.42.000000 to 1274419962 ( number of seconds since 1/1/1970)... is there a function that will do this for me?

Thanks so much!
Reply With Quote
  #2 (permalink)  
Old 05-21-10, 21:55
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
You may want to use secondsdiff UDF in this article.
DB2 Basics: Fun with Dates and Times

Example:
Code:
------------------------------ Commands Entered ------------------------------
SELECT timestamp_
     , SECONDSDIFF( timestamp_ , TIMESTAMP('1970-01-01-00.00.00.000000') ) integer_value
  FROM (VALUES TIMESTAMP('2010-05-21-05.32.42.000000') ) as q(timestamp_);
------------------------------------------------------------------------------

TIMESTAMP_                 INTEGER_VALUE
-------------------------- -------------
2010-05-21-05.32.42.000000    1274419962

  1 record(s) selected.
Reply With Quote
  #3 (permalink)  
Old 05-24-10, 12:18
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
thanks! will try it out...
Reply With Quote
  #4 (permalink)  
Old 05-25-10, 17:42
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Talking Good for any DB2 versions

Good for any:

Code:
SELECT current timestamp "Current Timestamp"
     , timestampDIFF
(2, char(current timestamp - TIMESTAMP('1970-01-01-00.00.00.000000')))  "Integer Value"
  FROM sysibm.sysdummy1;
Result:

Quote:
Current Timestamp........................... Integer Value
2010-05-25 17:41:07.752585.............. 1273945267
Lenny
Reply With Quote
  #5 (permalink)  
Old 05-25-10, 19:14
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Lenny,

You will find following descriptions in DB2 Basics: Fun with Dates and Times where I mentioned bofore in this thread.

Quote:
Sometimes, you need to know how the difference between two timestamps. For this, DB2 provides a built in function called TIMESTAMPDIFF(). The value returned is an approximation, however, because it does not account for leap years and assumes only 30 days per month.
Quote:
Using timestampdiff() is more accurate when the dates are close together than when they are far apart. If you need a more precise calculation, you can use the following to determine the difference in time (in seconds):
...
Quote:
For convenience, you can also create an SQL user-defined function of the above:
Code:
CREATE FUNCTION secondsdiff(t1 TIMESTAMP, t2 TIMESTAMP) 
...
Reply With Quote
  #6 (permalink)  
Old 05-26-10, 09:49
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Cool

Quote:
Originally Posted by tonkuma View Post
Lenny,

You will find following descriptions in DB2 Basics: Fun with Dates and Times where I mentioned bofore in this thread.
You right, tonkuma. But how I understood he want to use it as unique id.
For this reason TIMESTAMPDIFF is good enough.

Lenny
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On