SELECT * FROM HACPRP.EQ_POOL_ACT_VIEW WHERE ACTVTY_DT IN ( SELECT ACTVTY_DT FROM HACPRP.EQ_POOL_ACT_VIEW WHERE SUBSTR(ACTVTY_DT,5,2) < '03' UNION SELECT ACTVTY_DT FROM HACPRP.EQ_POOL_ACT_VIEW WHERE SUBSTR(ACTVTY_DT,1,2) < '09' AND SUBSTR(ACTVTY_DT,5,2) = '03' UNION SELECT ACTVTY_DT FROM HACPRP.EQ_POOL_ACT_VIEW WHERE SUBSTR(ACTVTY_DT,3,2) < '29' AND SUBSTR(ACTVTY_DT,1,2) = '09' AND SUBSTR(ACTVTY_DT,5,2)= '03' );
The reason I have to do it like this is that someone created the table with ACTVTY_DT as CHAR(06) instead of DATE. Why does the above SQL not run? It states that it is not suitable for the DB2 Manager. My aim is to simply select a list of entries that are less than the date given on the JCL job.
Requires Free Membership to View
Not having a DB2 system of my own to test on, the only thing I can see that DB2 might object to is the use of UNION in a subselect. You do not need to do it that way anyway, because you can rewrite this query as:
select *
from HACPRP.EQ_POOL_ACT_VIEW
where substr(ACTVTY_DT,5,2) < '03'
or (
substr(ACTVTY_DT,1,2) < '09'
and substr(ACTVTY_DT,5,2) = '03'
)
or (
substr(ACTVTY_DT,3,2) < '29'
and substr(ACTVTY_DT,1,2) = '09'
and substr(ACTVTY_DT,5,2) = '03'
)
For More Information
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, SQL Server, DB2, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
This was first published in October 2003
Join the conversationComment
Share
Comments
Results
Contribute to the conversation