Link to home
Start Free TrialLog in
Avatar of kenagy18
kenagy18

asked on

Change date range criteria in WHERE statement using DECODE

I am attempting to define a date range within the WHERE portion of my query statement in  Crystal Reports and am having difficulty altering the date range using DECODE.  I believe it is a syntax issue.  I have tried a couple options in the WHERE statement:

Option 1:
s.recd_date between DECODE('{?Pm-@genMethod}', 'BATCHNAME', '01-JAN-2000' and '31-DEC-2999', '{?Pm-@startDate}' and '{?Pm-@endDate}')

Option 2:
s.recd_date between DECODE('{?Pm-@genMethod}', 'BATCHNAME', '01-JAN-2000', '{?Pm-@startDate}') and DECODE('{?Pm-@genMethod}', 'BATCHNAME', '31-DEC-2999', '{?Pm-@endDate}')

Basically, if parameter Pm-@genMethod = 'BATCHNAME', I don't want to restrict by date range.  If parameter Pm-@genMethod equals anything else, I want to restrict the date range between (inclusive) Pm-@startDate and Pm-@endDate.

I've accomplished similar things with non-date fields, just can't get a handle on the date syntax.  

I am querying against an Oracle 9 database.

Thanks for the help.
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Hi kenagy18,

Your second option is the right syntax if you ignore the parameters.  You will most likely need to do a TO_DATE() on all four date values.

What error are you getting on Option 2?

lwadwell
Avatar of kenagy18
kenagy18

ASKER

Thanks.  I'm getting the following error with Option 2.

ORA-01858: a non-numeric character was found where a numeric was expected.

The query resides in a subreport and parameters Pm-@startDate and Pm-@endDate are both set based on a formula in the parent report using the formula:
datetime({@minDate})
datetime({@maxDate})

Is a TO_DATE() still required within the query?

Again...here is what I currently have.

s.recd_date between DECODE('{?Pm-@genMethod}', 'BATCHNAME', '01-JAN-2000', '{?Pm-@startDate}') and DECODE('{?Pm-@genMethod}', 'BATCHNAME', '31-DEC-2999', '{?Pm-@endDate}')



Also, in case it helps...

Simply adding s.recd_date = '01-JAN-2000' works without error.

Adding s.recd_date = '{?Pm-@startDate}' produces the same error I mentioned above.  Appears to be an issue with the parameter, not with the DECODE.
kenagy18,

I think you may need the TO_DATE as it is seeing/returning the dates in the DECODE as strings and not dates - hence the error.  With the s.recd_date = '01-JAN-2000' it is doing an implied TO_DATE().  I do not know what format the '{?Pm-@startDate}' will be interpreted as.  In any case, it is safe practice to do explicit date conversions.

Try this to see if this removes the error:
s.recd_date = TO_DATE('{?Pm-@startDate}','DD-Mon-YYYY')

If this is fine, then something like:
s.recd_date between DECODE('{?Pm-@genMethod}', 'BATCHNAME', TO_DATE('01-JAN-2000','DD-Mon-YYYY'), TO_DATE('{?Pm-@startDate}','DD-Mon-YYYY')) and DECODE('{?Pm-@genMethod}', 'BATCHNAME', TO_DATE('31-DEC-2999','DD-Mon-YYYY'), TO_DATE('{?Pm-@endDate}','DD-Mon-YYYY'))

lwadwell
I have been trying what you proposed and can not get the error removed.  I have been focused particularly on the following statement:

s.recd_date = TO_DATE('{?Pm-@startDate}','DD-Mon-YYYY')

I think you are on the right track.  I've been trying to change the expected date format but haven't had any luck yet.
SOLUTION
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, the error is still the same as earlier.

I have dropped the DateTime formula in Crystal.   The new parameters are now {?Pm-@minDate} and {?Pm-@maxDate}.

The minDate and maxDate fields are strings in the format of DD-MON-YYYY HH24:MI:SS
For example: 15-FEB-2007 00:00:00

I have just  tried the following:
s.recd_date = TO_DATE('15-FEB-2007 00:00:00', 'DD-MON-YYYY HH24:MI:SS')  - NO ERROR
s.recd_date = TO_DATE('{?Pm-@minDate}', 'DD-MON-YYYY HH24:MI:SS') - ERROR

The value of the parameter is 15-FEB-2007 00:00:00 so I'm confused why this isn't working.  This value is set in the parent report and I've dragged the parameter field on to the actual report to confirm this.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
kenagy18,

That is OK.  I would still put a TO_DATE around the 'fixed' dates as well.  Interpretation of strings like '01-JAN-2000' as dates can change based on environmental settings and it safest to get into the habit of always doing an explicit conversion.

lwadwell