Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Comparison of dates in DB2 for a join query.

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Shobs

New User


Joined: 21 Jan 2010
Posts: 6
Location: Pune

PostPosted: Tue Feb 16, 2010 12:54 pm    Post subject: Comparison of dates in DB2 for a join query.
Reply with quote

I need to write a join query on two tables where in I need to join the tables on two dates, however the format of the dates is different. In one table the format of the date is in DATE format e.g 2008-05-06 and the other table has date in format as '06MAY2008' (CHARACHTER (9)).How do I do the comparison? Thanks in Advance.
Back to top
View user's profile Send private message

ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Feb 16, 2010 2:44 pm    Post subject:
Reply with quote

You better go with a subquery rather than a join. This join will never be effective due to different data formats.
Back to top
View user's profile Send private message
Shobs

New User


Joined: 21 Jan 2010
Posts: 6
Location: Pune

PostPosted: Tue Feb 16, 2010 2:47 pm    Post subject: Reply to: Comparison of dates in DB2 for a join query.
Reply with quote

Can you please help me with the sub-query as I am new to DB2? Thanks.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Feb 16, 2010 3:05 pm    Post subject:
Reply with quote

Code:

SELECT * FROM TABLE A WHERE DATE_COL IN
(SELECT SUBSTR('06MAY2008',6,4)||'-'||
CASE SUBSTR('06MAY2008',3,3)
WHEN 'JAN' THEN '01'
WHEN 'FEB' THEN '02'
-----
-----
WHEN 'MAY' THEN '05'
-----
------
END
||'-'||SUBSTR('06MAY2008',1,2)
FROM TABLE B
)

Change '06MAY2008' with your date column in table B.
IMO the DB design could have been done in a better way.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts Join giving more records than expected Danielle.Filteau SYNCSORT 2 Thu Mar 09, 2017 3:28 am
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm
No new posts Omegamon for DB2 query ashek15 DB2 0 Wed Feb 15, 2017 11:25 am
No new posts Join of more than one table dibyendumandal11111 DB2 6 Mon Feb 13, 2017 11:36 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us