MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to compare a CHAR to a CURRENT DATE

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
prog_mario
Beginner


Joined: 08 Sep 2007
Posts: 86
Topics: 27

PostPosted: Fri Jul 30, 2010 9:13 am    Post subject: How to compare a CHAR to a CURRENT DATE Reply with quote

Hello, I need to perform the following query:

SELECT COUNT(*)
FROM TABLE
WHERE COLUMN >= CURRENT DATE - 6 MONTHS;

In this case COLUMN is a CHAR(8) formatted as YYYYMMDD.

Thanks.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Fri Jul 30, 2010 9:41 am    Post subject: Reply with quote

char(date_expression,ISO)
ISO

Code:

where column >= substr(char(current_date - 6 months, ISO),1,4) ||
                substr(char(current_date - 6 months, ISO),6,2) ||
                substr(char(current_date - 6 months, ISO),9,2)

above is untested.

easily tested in spufi:
Code:

select substr(char(current_date - 6 months, ISO),1,4) ||
        substr(char(current_date - 6 months, ISO),6,2) ||
        substr(char(current_date - 6 months, ISO),9,2)
     , length(substr(char(current_date - 6 months, ISO),1,4) ||
              substr(char(current_date - 6 months, ISO),6,2) ||
              substr(char(current_date - 6 months, ISO),9,2)
               )
from sysibm.sysdummy1


the above should return
yyyymmdd 8
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
jim haire
Beginner


Joined: 30 Dec 2002
Posts: 140
Topics: 40

PostPosted: Fri Jul 30, 2010 9:47 am    Post subject: Reply with quote

Untested:

Code:

SELECT count(*) FROM TABLE
   WHERE SUBSTR(COLUMN,1,4) || '-' || SUBSTR(COLUMN,5,2) || '-' ||
            SUBSTR(COLUMN,7,2) >= CHAR(CURRENT DATE - 6 MONTHS)
Back to top
View user's profile Send private message
prog_mario
Beginner


Joined: 08 Sep 2007
Posts: 86
Topics: 27

PostPosted: Fri Jul 30, 2010 9:56 am    Post subject: Reply with quote

Thanks a lot everybody. It worked.
_________________
The more I learn, the more I want to learn.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Fri Jul 30, 2010 9:58 am    Post subject: Reply with quote

jim, you sql works,
but you are executing a multi-substring on the column for every row,
where as I attempted to only execute the scalar functions 1 time.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Fri Jul 30, 2010 10:21 am    Post subject: Reply with quote

prog_mario,

Here is an alternative way to get the desired results. Untested SQL
Code:

SELECT COUNT(*)
  FROM TABLE
  WHERE COLUMN >= CHAR(REPLACE(CHAR(CURRENT DATE - 6 MONTHS),'-',''));


Modified sql from this topic

http://www.mvsforums.com/helpboards/viewtopic.php?t=11000&highlight=replace


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Fri Jul 30, 2010 10:47 am    Post subject: Reply with quote

Kolusu,

mind like a steel trap, you have!
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group