Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - Return shortest string value in a set of rows

Tags:

sql

oracle

plsql

I'm trying to write a query that returns the shortest string value in the column. For ex: if ColumnA has values ABCDE, ZXDR, ERC, the query should return "ERC". I've written the following query, but I'm wondering if there is any better way to do this?

The query should return a single value.

select distinct ColumnA from
(
  select ColumnA, rank() over (order by length(ColumnA), ColumnA) len_rank 
    from TableA where ColumnB = 'XXX'
)
where len_rank <= 1
like image 909
MNIK Avatar asked Feb 03 '23 04:02

MNIK


1 Answers

How about:

select ColumnA
from
(
  select ColumnA
  from tablea
  order by length(ColumnA) ASC
)
where rownum = 1
like image 109
IMHO Avatar answered Feb 05 '23 18:02

IMHO