For a report to function properly, I have to convert the date parameters in a working Oracle SQL query with strings for a report. While this may sound odd - these are dates after all - the use of strings is a requirement for the report to run in our hosted database. The report runs beautifully in Crystal Reports with the date parameters, and be advised that the curly braces {} are how Crystal Reports defines parameters.
The challenge is converting the current syntax to a character-based syntax. Here is an excerpt of the larger query; the {?01_START_DT} is a date parameter of format MM/DD/YYYY (e.g., 07/31/2020):
SELECT U."UnitNumber" AS UNIT_NO
, U."UsingDepartment" AS USE_DEPT
, U."UnitStatus" AS STATUS
, (
SELECT H.USAGE
FROM UNIT_HIST H
WHERE U."UNITID" = H.UNIT_ID
AND H.COMPANY = 'OKS'
AND FISC_PD = CASE WHEN EXTRACT(MONTH FROM {?01_START_DT}) >= 7
THEN TO_CHAR(ADD_MONTHS({?01_START_DT}, 12), 'YYYY') || TO_CHAR(ADD_MONTHS({?01_START_DT}, -6), 'MM')
ELSE TO_CHAR({?01_START_DT}, 'YYYY') || TO_CHAR(ADD_MONTHS({?01_START_DT}, 6), 'MM')
END
FROM VIEW_ALL_UNITS U
Straight-forward to replace the parameter with a string {?START_DT} of the form MM/DD/YYYY (e.g., '07/31/2020', entered without quotes) as follows:
SELECT U."UnitNumber" AS UNIT_NO
, U."UsingDepartment" AS USE_DEPT
, U."UnitStatus" AS STATUS
, (
SELECT H.USAGE
FROM UNIT_HIST H
WHERE U."UNITID" = H.UNIT_ID
AND H.COMPANY = 'OKS'
AND FISC_PD = CASE
WHEN EXTRACT(MONTH FROM TO_DATE({?START_DT}, 'MM/DD/YYYY') >= 7
THEN TO_CHAR(ADD_MONTHS(TO_DATE({?START_DT}, 'MM/DD/YYYY'), 12), 'YYYY') || TO_CHAR(ADD_MONTHS(TO_DATE({?START_DT}, 'MM/DD/YYYY'), -6), 'MM')
ELSE TO_CHAR(TO_DATE({?START_DT}, 'MM/DD/YYYY'), 'YYYY') || TO_CHAR(ADD_MONTHS(TO_DATE({?START_DT}, 'MM/DD/YYYY'), 6), 'MM')
END
FROM VIEW_ALL_UNITS U
Except this produces a ORA-01858 "non-numeric character" error!
I've tried sooo many things without success:
- Changes to the input string (even though it matches the format)
- Adding the time component to the format and the input string
- Adding the optionally NLZ portion of TO_DATE
- Simplifying the line by removing the EXTRACT and MONTH statements (for troubleshooting)
- Changing the CASE expression (for troubleshooting) to:
FISC_PD = CASE WHEN TO_DATE({?START_DT}, 'MM/DD/YYYY') = TO_DATE('07/01/2020', 'MM/DD/YYYY') THEN
Needless to say the error persists, regardless of what I try.
As I alluded to previously, this queries a database on a hosted system to which I do not have physical or electronic access. These queries are against a reporting database. The error does not provide a line number or any other information useful to troubleshooting.
Any ideas what may be causing this issue?