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 > Oracle To DB2 todate(..) >= SUBSTR(date...)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-09-04, 04:38
gopidba gopidba is offline
Registered User
 
Join Date: Nov 2004
Posts: 26
Question Oracle To DB2 todate(..) >= SUBSTR(date...)

Hi,

I want to convert the following Oracle query in to a DB2 query.

select *
FROM Vacation vac, BusinessEmployee emp
where
to_date(to_char(vac.startdate, 'MM/DD/YYYY'), 'MM/DD/YYYY') >= SUBSTR(SYSDATE, 1, 10)

The above query is to select all those rows that have the date value higher than the current date.

This is part of a complex query and is executing well i Oracle.

In DB2 i tried,

SELECT
TIMESTAMP(SUBSTR(CHAR(CHAR(DATE(vac.startdate), USA)), 7,4) || '-' || SUBSTR(CHAR(CHAR(DATE(vac.startdate), USA)), 1,2) || '-' || SUBSTR(CHAR(CHAR(DATE(vac.startdate), USA)), 4,2) || ' 00:00:00') >=
SUBSTR(CURRENT TIMESTAMP, 1, 10)
FROM Vacation vac, BusinessEmployee emp

i got the error,
[IBM][CLI Driver][DB2/LINUX] SQL0180N The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007


The left & right side of the comparison is working well when executed individually. The issue is in the comparison of them( a>= b)

Also tell me whether Oracle is doing any implicit datatype conversion to compare the different datatypes here?.

-gopidba

Last edited by gopidba; 12-09-04 at 08:28.
Reply With Quote
  #2 (permalink)  
Old 12-09-04, 04:53
okman okman is offline
Registered User
 
Join Date: Oct 2004
Posts: 4
Since you're only interested about "did it happen before or after", why not do this:

Code:
select * 
from Vacation vac, BusinessEmployee emp
where timestampdiff(2, char(vac.startdate - current timestamp)) > 0
Converting a date to character representation and then comparing it seems a bit wasteful... Also - are you sure your Oracle SQL is valid - you are selecting the date as 'Month-Day-Year' instead of a comparable 'Year-Month-Day' format. I would re-write the Oracle select as

Code:
select *
FROM Vacation vac, BusinessEmployee emp
where vac.startdate >= sysdate
Reply With Quote
  #3 (permalink)  
Old 12-09-04, 06:13
gopidba gopidba is offline
Registered User
 
Join Date: Nov 2004
Posts: 26
Question

Hi okman,

Thanks for the reply.

I too understand the query given is not straight forward.

But that is the existing query and we need to migrate it to DB2.

My actual query is the one below.


SELECT *
FROM Vacation vac, BusinessEmployee emp
WHERE
vac.businessperson = emp.oid
AND (to_date(to_char(vac.startdate, 'MM/DD/YYYY'), 'MM/DD/YYYY') >= SUBSTR(SYSDATE, 1, 10)
OR to_date(to_char(vac.enddate, 'MM/DD/YYYY'), 'MM/DD/YYYY') >= SUBSTR(SYSDATE, 1, 10))
ORDER BY emp.surname


-gopidba
Reply With Quote
  #4 (permalink)  
Old 12-09-04, 07:19
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
This should work

Code:
SELECT * 
FROM	Vacation vac, BusinessEmployee emp 
WHERE	vac.businessperson  = emp.oid
 AND	(
	vac.startdate  >= CURRENT DATE 
	OR	
	vac.enddate  >= CURRENT DATE
	)
ORDER BY emp.surname;
But in DB2 you can also slow slow it down a lot by first converting the dates to strings

Quote:
Originally Posted by gopidba

SELECT *
FROM Vacation vac, BusinessEmployee emp
WHERE
vac.businessperson = emp.oid
AND (to_date(to_char(vac.startdate, 'MM/DD/YYYY'), 'MM/DD/YYYY') >= SUBSTR(SYSDATE, 1, 10)
OR to_date(to_char(vac.enddate, 'MM/DD/YYYY'), 'MM/DD/YYYY') >= SUBSTR(SYSDATE, 1, 10))
ORDER BY emp.surname

Wim
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