0

I have a column in my table that stores max 2 characters. For eg. B,AK,SE etc.

Suppose I have two rows in that table, one having 'K' and other having 'BA' as a value.

Requirement in my project is such that I have to get the maximum value from the table based on the no. of characters in case there is a difference in the length. In this case, maximum value will be 'BA' not 'K'.

But when I use 'order by column desc' with rownum = 1 in my query, 'K' is returned as rownum 0th value.

If I have values 'D','BA','AK','C' then output should be 'BA','AK','C','D' Just want to know if there is any way I can fulfill my requirement?

2
  • what do you mean by rownum = 0? don't you mean rownum = 1? Commented Jun 26, 2012 at 7:17
  • Sorry typo mistake it's rownum=1 Commented Jun 26, 2012 at 12:32

2 Answers 2

3

Simplest way could be:

ORDER BY length(<col_name>) desc

UPDATE - if you need to order by the value also, you can do:

ORDER BY length(<col_name>) desc, <col_name> desc
Sign up to request clarification or add additional context in comments.

2 Comments

but by doing so, I will not be able to sort single/double character values. For eg. 1) 'F' and 'H' 2) 'DF' and 'HG'
edited my explanation little bit to make it more understandable
0

try :

select * from (
  select row_number() over(order by length(col_name) desc, col_name desc) rn, col_name
  from tab_name
) where rn = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.