fhg
Volume 8, Number 25 -- July 9, 2008

A Handy SQL Timestamp Function

Published: July 9, 2008

Hey, Ted:

I have found a great tool for working with timestamp data. I have used it for performance tracking and analyzing data intervals. It's SQL's Timestampdiff function, and I think you should tell readers of Four Hundred Guru about it.

Timestampdiff returns the difference between two timestamps in the interval of your choice. The first parameter for the timestampdiff function is the interval to use. Valid values for this parameter are:


1

Fractions of a second

2

Seconds

4

Minutes

8

Hours

16

Days

32

Weeks

64

Months

128

Quarters

256

Years

 

The second parameter is used to subtract the two timestamp values. I cast the result as char(22).

Here's an example:

with STATISTICS as
  ( select count(*) as TRANSMITTED,
           min(TRANSMIT_TIMESTAMP) as START_TIMESTAMP,
           max(TRANSMIT_TIMESTAMP) as END_TIMESTAMP
      from XMITLOG
     where date(TRANSMIT_TIMESTAMP) = current_date)

select TRANSMITTED, START_TIMESTAMP, END_TIMESTAMP,
       timestampdiff(4,
          cast( (END_TIMESTAMP - START_TIMESTAMP) as char(22)))
          as ELAPSED_MINUTES
  from STATISTICS

The result set looked like this:


TRANSMITTED

START_TIMESTAMP

END_TIMESTAMP

ELAPSED_MINUTES

1,138

2008-03-19-05.46.51.473676

2008-03-19-06.15.14.241519

28

 

--Jeff


Thanks for bringing this powerful function to our attention, Jeff. One of the problems we've faced for so many years by storing dates and times in numeric and alpha fields is the difficulty of date/time arithmetic. It's marvelous to have such wonderful tools to work with these days.

--Ted




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
HELP/SYSTEMS

SEQUEL™ is the leading business intelligence solution for the IBM® System i™.

                      · Executive Dashboards with Gauges for Critical Business Metrics
                      · Graphical Query and Reporting
                      · Drill-Down Data Analysis
                      · Multi-Platform Database Access
                      · Powerful Scripting with CL-like Programming
                      · Secure Web Access

SEQUEL meets all your System i data access and analysis requirements.

Learn more now.


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

ARCAD Software:  Dynamic, world-class ALM on and around the System i
PowerTech:  Interact 3.0, the leading solution for real-time monitoring of IBM i security events
COMMON:  Join us at the Focus 2008 workshop conference, October 5 - 8, in San Francisco, California


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
Getting Started with PHP for i5/OS: List Price, $59.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95


 
The Four Hundred
The Power Systems 550 M50 Versus Its Predecessors

IBM Rejiggers Development Tools on Entry Power 520 i Editions

Job Word Cloud Redux: The AS/400 Sees Some Improvement

As I See It: The Digital Leader

WebSphere Portal Remains the Industry Leader, Says Gartner

The Linux Beacon
Xandros Continues Linux Buildout with Linspire Buy

Red Hat's Profit Growth Stalls in Fiscal Q1, RHEL MRG Launched

Gartner Revises HP's Server Sales Downward for Q1

As I See It: The Digital Leader

The Relational Database Market Grows Decently in 2007

Four Hundred Stuff
Vision Solutions Continues HA Evolution with ORION 6.0

CCSS Addresses MQSeries Monitoring Pain on i OS

NGS Launches Another BI Product for i OS

AMB Hooks Data Quality Tool into IBM DataStage

Varsity's i OS Shipping Software Certified by UPS

Big Iron
IBM v PSI: The Operation Was a Success, But the Patient Died

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
July 5, 2008: Volume 10, Number 27

June 28, 2008: Volume 10, Number 26

June 21, 2008: Volume 10, Number 25

June 14, 2008: Volume 10, Number 24

June 7, 2008: Volume 10, Number 23

May 31, 2008: Volume 10, Number 22

The Windows Observer
Bye Bye Bill

Supercomputers' Need for Speed Satisfied with Windows HPC Server '08

Patches? We Don't Need No Stinkin' Patches: Survey

Windows Boss Discusses 'Downgrade Rights' for XP, Windows 7 Compatibility

The Top 500 Super Ranking Now Counts Watts as Well as Flops

The Unix Guardian
HP Donates the Guts of Tru64 Unix's File System to Linux

Fujitsu Lands Monster Unix Deal with China Mobile

Virtual Servers Keep On A Rollin', Thanks to uptime software

As I See It: Flights of Fancy

Agilysys Hires JPMorgan for Possible Sale

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
COMMON


Printer Friendly Version


TABLE OF CONTENTS
PHP and the Zend Framework

A Handy SQL Timestamp Function

Reader Feedback: More on Preventing System Startup, Attention Lights, and Adding Drives

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
IFF ACTIVE Equivalent in CL

Printer Problem

Capture Sort File and Copy to Database File

SNMP Traps on i5OS

Java Messages

Copying recs from a subfile to a file and keeping highlights





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement