Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, rajesh.

Asked: April 13, 2002 - 10:13 pm UTC

Last updated: May 22, 2004 - 5:13 pm UTC

Version: 817

Viewed 1000+ times

You Asked



hi tom

I have to order the following characters

231
22
5678
12/02-03
2/02-03
10/02-03
as
22
231
5678
2/02-03
10/02-03
12/02-03

is that is possible. please help me
sincerely
rajesh

and Tom said...

sure, anything is possible.

I cannot provide you a 100% answer because I don't know your data. I made the assumption that:

a) if the string doesn't have a / in it, its a number that I can to_number
b) 2/02-03 is a date in dd/mm format, not sure what the -03 is. I ignored it.

So, if those assumptions are not correct -- you should be able to take the method below and CHANGE it yourself into a method that works. What is important is you see the technique, not that you get the exact answer:

ops$tkyte@ORA817DEV.US.ORACLE.COM> select * from t
2 order by decode( sign(instr(x,'/')), 1, 2, 1 ),
3 decode( sign(instr(x,'/')), 1, 0, to_number(x) ),
4 decode( sign(instr(x,'/')), 1, to_date(substr(x,1,instr(x,'-')-1),'dd/mm'), to_date(null) )
5 /

X
--------------------
22
231
5678
2/02-03
10/02-03
12/02-03

6 rows selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM>

So, that order by is really:

order by
<if the string has a slash in it, we want those "2cnd" so return 2, else
return 1>,

<if the string has a slash in it, return 0, else to_number the string so
Oracle will sort the string as a number. Remember, all strings with
slashes will have a 2 as the first order by and a 0 as the second now>

<if the string has a slash in it, return to_date on the first part, else
return null>


You will probably have to work on that last part given I don't know your rules for sorting that.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

thank you tom

A reader, May 05, 2002 - 5:12 am UTC

hi tom

thank u for your wonderful answer.

(b)
[2/02-03 is a date in dd/mm format, not sure what the -03 is. I ignored it.]

as you had assumed the number before the / is not indicating the month. and it can be up to 9999.
ie 9999/02-03.
in such situation what can i do

sincerely

rajesh





Tom Kyte
May 05, 2002 - 9:27 am UTC

then just parse differently.  


ops$tkyte@ORA817DEV.US.ORACLE.COM> select x,
  2             decode( sign(instr(x,'/')), 1, 2, 1 ) c1,
  3         decode( sign(instr(x,'/')), 1, 0, to_number(x) ) c2,
  4         decode( sign(instr(x,'/')), 1, to_number(substr(x,1,instr(x,'/')-1)) ) c3,
  5         decode( sign(instr(x,'/')), 1, to_number(substr(x, instr(x,'/')+1, instr(x,'-')-instr(x,'/')-1 )) ) c4,
  6             decode( sign(instr(x,'-')), 1, to_number(substr(x,instr(x,'-')+1)) ) c5
  7    from t
  8  /

X                  C1         C2         C3         C4         C5
---------- ---------- ---------- ---------- ---------- ----------
231                 1        231
22                  1         22
5678                1       5678
12/02-03            2          0         12          2          3
2/02-03             2          0          2          2          3
10/02-03            2          0         10          2          3

6 rows selected.


and order by whatever you need.
 

coversion of string

Sandeep Manikrao Deokar, May 22, 2004 - 7:12 am UTC

hi sir
I am having one String which i will get from database like
and this i will get as single row
1000^100~1001^101~1002^102 ...... this can be upto n numbers as well

and i want result like
1000 100
1001 101
1002 102
..
i want this using one single SQL query how it is possible
Thanx in advance


Tom Kyte
May 22, 2004 - 5:13 pm UTC