Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Oracle Sort string (numbers) and (letters with numbers)

Tags:

sorting

oracle

I am new to oracle and I have a problem. I have a column named file_id.

When I do an order by it sorts strings such as

1
1 
10 
100 
11 
11
110 
114
12
300 
31
4200
B14
B170
B18

edit: I would like it to sort this way.

1
1
10
11
11
12
31
100
300
4200
B14
B18 
B170

The answer below works perfectly. Only other problem I ran into now..I have records that are blank. How could I make the blank records order at the end?

1 
1 
10 
11 
11 
12 
31 
100 
300 
4200 
BLANK 
BLANK 
BLANK 
BLANK 
BLANK 
B14 
B18 
B170

Thank you for your help.

like image 385
user2199531 Avatar asked Mar 22 '13 14:03

user2199531


People also ask

How do I sort a string in Oracle?

Introduction to Oracle ORDER BY clause To sort data, you add the ORDER BY clause to the SELECT statement as follows: SELECT column_1, column_2, column_3, ... FROM table_name ORDER BY column_1 [ASC | DESC] [NULLS FIRST | NULLS LAST], column_1 [ASC | DESC] [NULLS FIRST | NULLS LAST], ...

How do I sort a varchar number in SQL?

'LPAD(lower(column_name))' is used to sort the varchar field numerically in MySQL. Let us see an example. Firstly, we will create a table. The CREATE command is used to create a table.

Can we store number in VARCHAR2?

The biggest reason is that when stored in a VARCHAR2 datatype, you can retain your leading zeros, whereas in a NUMBER datatype, you cannot.

How do I sort numbers in SQL?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.


2 Answers

select column 
from table
order by 
  regexp_substr(column, '^\D*') nulls first,
  to_number(regexp_substr(column, '\d+'))

fiddle

like image 100
Egor Skriptunoff Avatar answered Oct 03 '22 13:10

Egor Skriptunoff


This is an old question, but it was the first hit on google so I thought I'd share an alternative solution:

select column
from table
order by 
  LPAD(column, 10)

The LPAD function pads the left-side of the string with spaces so that the results will be sorted numerically. This works for non-numeric values, and null values will be sorted last. This works well if you know the maximum length of the strings to be sorted (you may need to adjust the second parameter to suit your needs).

Source: http://www.techonthenet.com/oracle/questions/sort1.php

EDIT:
I noticed that while my solution works well for my case, the output is slightly different from the accepted answer (http://www.sqlfiddle.com/#!4/d935b8/2/0):

1
1
10
11
11
12
31
100
110
114
300
A14
A18
4200
A170
(null)
(null)

4200 should come after 300. For my situation this is good enough, but this may not always be the case.

like image 36
Northmoor Avatar answered Oct 03 '22 14:10

Northmoor