Solved

timestamp function in db2

Posted on 2009-07-13
4
3,327 Views
Last Modified: 2012-08-14
I found strange things when using timestamp funtion in writing a query for db2 sql that had the following clause in the sql statement
AND   timestamp('2009-06-01','00:00:00') <=  startdatetime AND   StartDateTime < timestamp(date('2009-06-30') + 1 day,'00:00:00')

same above statement when it was written as follows
AND startdatetime >= '2009-06-01-00.00.00' and startdatetime <= '2009-06-30-00.00.00' - produced different results  - first one produced more aggregate records for evaluation then the second - what differences that db2 does in using a timestamp fuction as against direct comparisons of datetime field? (startdatetime is defined as timestamp of length 10)
0
Comment
Question by:mahjag
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:daveslash
ID: 24843866

On one clause you have "less-than", but on the other clause you have "less-than-or-equal-to"

HTH,
DaveSlash


0
 
LVL 45

Expert Comment

by:Kdo
ID: 24843951
Hi mahjag,

What Dave said.

Also, the format of the datetime is incorrect.  The value '2009-06-01-00.00.00'  should be '2009-06-01.00.00.00'.  Note that a period should separate the date and time values, not a hyphen.  An improperly formatted datetime will affect the comparison in an ugly way.


Good Luck,
Kent
0
 
LVL 18

Accepted Solution

by:
daveslash earned 125 total points
ID: 24844353

That's realy interesting.  ( see below )

On DB2 for i, this works:
 

select *

from   deleteme

where  aTS >= '2009-07-01-00.00.00'

  and  aTS <= '2009-07-31-00.00.00'

;
 

But, this doesn't:
 

select *

from   deleteme

where  aTS >= '2009-07-01.00.00.00'

  and  aTS <= '2009-07-31.00.00.00'

;
 

[SQL0180] Syntax of date, time, or timestamp value not valid.

Open in new window

0
 
LVL 45

Expert Comment

by:Kdo
ID: 24845151
Hi Dave,

Wow.  I've had scads of queries fail due to badly formatted date, time and/or datetime values.  I've never seen the behavior where a timestamp was accepted in that format.

Of course, I'm an LUW kind of guy.   :)    (Where a full timestamp really needs microseconds ('.000000') appended after the time.)


Kent
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now