Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

php - How to sort by numbers first with Oracle SQL query?

I have this table with a 'title' field which is varchar2 and I want to select all rows and sort them first by number and then by the alphabet as it normally happens.

For instance, I currently get this using a simple ORDER BY title in the end:

  • Abc
  • Def
  • 321

But I want this:

  • 321
  • Abc
  • Def

The weird thing is that SQL Developer shows the "right" order, with numbers first. But on my app (PHP using OCI8) it shows numbers last.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not an Oracle expert, but you are supposed to be able to do it without altering the session with

SELECT * FROM my_data SORT by NLSSORT(title,’NLS_SORT=BINARY_AI’)

where you can change the NLS_SORT= to fit your needs (here are the list of values)

Keep in mind that docs says that this will force table scan, so it might be beneficial to filter them first (but if you are selecting all the table scan is what you are going to use anyway).

The reason why SQL Developer exhibits different behaviour is probably because it changes the session.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...