I'm trying to order a varchar column first numerically, and second
alphanumericall y using the following SQL:
SELECT distinct doc_number
FROM doc_line
WHERE product_id = 'WD' AND doc_type = 'O'
ORDER BY CASE WHEN IsNumeric(doc_n umber) = 1
THEN CONVERT(FLOAT, doc_number)
ELSE 999999999
END,
CASE WHEN IsNumeric(doc_n umber) = 1
THEN 'ZZZZZZZZZ'
ELSE doc_number
END;
When try executing this statement, I get the following error:
Server: Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is
specified.
If I take the "distinct" out, it works just fine, except for the fact that I
get many duplicates.
Does anyone have any suggestions?
Thanks,
Frank
alphanumericall y using the following SQL:
SELECT distinct doc_number
FROM doc_line
WHERE product_id = 'WD' AND doc_type = 'O'
ORDER BY CASE WHEN IsNumeric(doc_n umber) = 1
THEN CONVERT(FLOAT, doc_number)
ELSE 999999999
END,
CASE WHEN IsNumeric(doc_n umber) = 1
THEN 'ZZZZZZZZZ'
ELSE doc_number
END;
When try executing this statement, I get the following error:
Server: Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is
specified.
If I take the "distinct" out, it works just fine, except for the fact that I
get many duplicates.
Does anyone have any suggestions?
Thanks,
Frank
Comment