The following query works as long as a parameter value is entered for IncomeYear:
SELECT SUM( "IncomeAmount" )
FROM "Income"
WHERE YEAR("IncomeDate") =
CASE WHEN :IncomeYear <> '' THEN :IncomeYear ELSE YEAR("IncomeDate") END
Otherwise, the following error occurs:
SQL Status: 37000
Error code: -16
Wrong data type: java.lang.NumberFormatException: For input string: ""
Can anyone explain to me why I am receiving this error?
[Solved] Error with optional input parameter
[Solved] Error with optional input parameter
Last edited by HildyD on Mon Jul 17, 2017 3:14 pm, edited 1 time in total.
Apache OpenOffice 4.1.3
Windows 10
Windows 10
-
- Volunteer
- Posts: 1549
- Joined: Wed Jun 24, 2015 12:56 am
- Location: Colorado, USA
Re: Error with optional input parameter
Looking at your query I noticed that the parameter dialog returns a Numeric type if numbers are typed into the dialog and your query functions normally.
When nothing or for that matter any non numeric character(s) is entered into the parameter dialog it returns a String. Which results in
The error message you are seeing is the result of trying to equate a numeric value to a string.
Based on that I unsuccessfully tried to Cast the parameter dialog output into an Integer.
Going the other direction I arrived atThis works for all numeric values and it doesn't result in an error message when nothing is typed into the dialog.
This may not work exactly as intended, but hopefully it will show you a way forward.
When nothing or for that matter any non numeric character(s) is entered into the parameter dialog it returns a String. Which results in
Code: Select all
WHERE YEAR("IncomeDate") = 'Some String'
Based on that I unsuccessfully tried to Cast the parameter dialog output into an Integer.
Going the other direction I arrived at
Code: Select all
WHERE CAST(YEAR("IncomeDate") AS VARCHAR(4)) = IFNULL( CAST(:IncomeYear AS VARCHAR(4)), CAST(YEAR("IncomeDate") AS VARCHAR(4)))
This may not work exactly as intended, but hopefully it will show you a way forward.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
-
- Volunteer
- Posts: 1549
- Joined: Wed Jun 24, 2015 12:56 am
- Location: Colorado, USA
Re: Error with optional input parameter
I just went back and modified you code once more to
and it seems to return the correct results.
Code: Select all
WHERE CAST( YEAR( "IncomeDate" ) AS VARCHAR ( 4 ) ) =
CASE WHEN CAST( :IncomeYear AS VARCHAR ( 4 ) ) <> '' THEN CAST( :IncomeYear AS VARCHAR ( 4 ) ) ELSE CAST( YEAR( "IncomeDate" ) AS VARCHAR ( 4 ) ) END
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
Re: Error with optional input parameter
Thank you so much for your help! I modified my query as follows and it works:
SELECT SUM( "IncomeAmount" )
FROM "Income"
WHERE cast(YEAR("IncomeDate") as varchar(4)) =
CASE WHEN :IncomeYear <> '' THEN :IncomeYear ELSE cast(YEAR("IncomeDate") as varchar(4)) END
I was not required to cast the input parameter.
SELECT SUM( "IncomeAmount" )
FROM "Income"
WHERE cast(YEAR("IncomeDate") as varchar(4)) =
CASE WHEN :IncomeYear <> '' THEN :IncomeYear ELSE cast(YEAR("IncomeDate") as varchar(4)) END
I was not required to cast the input parameter.
Apache OpenOffice 4.1.3
Windows 10
Windows 10