× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi Justin

I just googled for *conditional order by* and got an interesting hit that might help.

In a CASE, the results must be the same data type, I saw. It wasn't IBM i, but the rule still holds.

One solution was to use multiple CASE, something like this - I'm just taking a wild shot here - each CASE can have its own data type -

order by
(case :H <> :H then NumericCol end)
(case :H = :H then CharCol end)

Your USING clause would add a couple values.

Here's a link to the page - https://stackoverflow.com/questions/15621609/t-sql-conditional-order-by

Regards
Vern

On 10/24/2024 7:14 AM, Justin Taylor wrote:
I have this SQL:
select *
from TableA A left join TableB B
on A.ID = B.ID
where A.Column = :H
order by case when :H <> :H
then NumericCol --decimal(7) TableA
else CharCol end ; --char(6) TableA

It runs fine as long as the result set only includes numbers for CharCol,
but it fails with a conversion error if CharCol contains letters. I
commented out the ORDER BY and it runs. I put the ORDER BY back and it
failed again, so I know the ORDER BY is the problem. It's casting CharCol
to decimal(7). I tried flipping the condition in the ORDER BY, but that
didn't make a difference.

1. I'm curious why it's doing the cast.
2. Is there any relatively painless way to stop it?

Thx

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.