Comparing dates

A forum for discussing DataStage<sup>®</sup> basics. If you're not sure where your question goes, start here.

Moderators: chulett, rschirm, roy

Post Reply
devidotcom
Participant
Posts: 247
Joined: Thu Apr 27, 2006 6:38 am
Location: Hyderabad

Comparing dates

Post by devidotcom »

I have to compare two dates. My source fields are varchar(10) columns.
e.g. in the format 2003-12-10

I order to compare I plan to change the format to YYYYMMDD format. How do I remove '-' in them?
Is it that after removing them I need to convert these varchar field to decimal using StringToDecimal function. So that I can use greater than or less than operations.
Maveric
Participant
Posts: 388
Joined: Tue Mar 13, 2007 1:28 am

Post by Maveric »

Don't have to convert it to Decimal. Convert the varchar to date using StringtoDate and compare them directly.
devidotcom
Participant
Posts: 247
Joined: Thu Apr 27, 2006 6:38 am
Location: Hyderabad

Post by devidotcom »

So I just use the StringToDate function for both the input columns and compare using the following expression

svDate1=StringToDate(input.Date11)
svDate2=StringToDate(input.Date22)
svComp=svDate1>svDate2

where svDate1, svDate2 are date datatype
Is this right?
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Don't need to be converted at all, they will compare fine (dashes and all) as long as month and day are always two digits.
-craig

"You can never have too many knives" -- Logan Nine Fingers
Maveric
Participant
Posts: 388
Joined: Tue Mar 13, 2007 1:28 am

Post by Maveric »

I did not know that a direct compare will work. As craig suggested make sure the date and month fields are always 2 characters and do a direct compare. As it is for StringtoDate to work you will have to make sure that the length of the data is 10 characters. So

If len(input.date11) = 10 and len(input.date22) = 10 then compare logic else ().

You would have to take care of the else condition though if the lengths are not 10.
devidotcom
Participant
Posts: 247
Joined: Thu Apr 27, 2006 6:38 am
Location: Hyderabad

Post by devidotcom »

Hi All,

Thanks it works fine now. I converted them to dates and compared using greater than or lesser than signs.

Thanks
Post Reply