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 

Compare date field with Varchar

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


Joined: 03 Oct 2005
Posts: 70
Topics: 34

PostPosted: Thu Feb 18, 2010 1:55 pm    Post subject: Compare date field with Varchar Reply with quote

Hi,

I have a DB2 table where one of the colum is Text_Area (Datatype: Varchar). This Colum stores the date field(format: YYMMDD).I want to query this table to fetch all the rows greater than
a particular date.
Ex:
Select *
From TABLEA
where Text_Area > Current Date

Since Text_Area is a varchar field, I am not able to do the date comparision.
Any ideas please...

Thanks!
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


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

PostPosted: Thu Feb 18, 2010 2:06 pm    Post subject: Reply with quote

THRIVIKRAM,

How long is the varchar column defined? post a sample of the column data

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


Joined: 03 Oct 2005
Posts: 70
Topics: 34

PostPosted: Thu Feb 18, 2010 2:50 pm    Post subject: Reply with quote

Hi,

I got the solution. Below is the query I used:
(Test_Area is 40 bytes and last 6bytes is the date)
Code:

SELECT *
  FROM TABLEA
  WHERE 
 (SUBSTR(Test_Area,34,2) > '50'                       
   AND '19'|| SUBSTR(Test_Area,34,6) <= '20100203'     
   OR  SUBSTR(Test_Area,34,2) < '50'                   
   AND  '20'|| SUBSTR(Test_Area,34,6) <= '20100203')   


Thanks!!
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


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

PostPosted: Thu Feb 18, 2010 6:25 pm    Post subject: Reply with quote

THRIVIKRAM,

I believe even this would give you the desired results
Code:

SELECT *                                                             
  FROM TABLE                                                     
  WHERE (CASE WHEN SUBSTR(TEST_AREA,34,2) > '50'                   
         THEN CHAR('19') || SUBSTR(TEST_AREA,34,6)                   
         ELSE CHAR('20') || SUBSTR(TEST_AREA,34,6) END) <= '20100203'
   ;


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


Joined: 03 Oct 2005
Posts: 70
Topics: 34

PostPosted: Fri Feb 19, 2010 10:03 am    Post subject: Reply with quote

Thanks Kolusu!
Back to top
View user's profile Send private message Send e-mail
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