Oracle version : 10g
I’m trying to use the MIN function to find the minimum/lowest value of a dataset. The column I’m performing MIN on is a VARCHAR2 column. This column will either contain a numeric value, or a value of ‘–‘ which represents that the value is not applicable.
When performing the MIN() function on the column, the ‘–‘ is always returned.
I want to find a way to exclude the ‘–‘ from being calculated in the MIN statement. I can’t use the WHERE clause in the statement to filter out columns with a ‘–‘ because that would exclude valid data.
Note: This is a huge legacy query (500+ lines) so re-writing this massive query is not really an option.
MIN(CASE WHEN ColX = '--' THEN NULL ELSE ColX END)This is valid in SQL Server and I think will probably work on Oracle as well.
As a side note, never ever store numeric data in string fields.
It’s inefficient from a space perspective and you will get some weird results when doing inequality comparisons.
If you have these rows in your table:
The
MAXvalue will be1and theMINvalue will be009