Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order by 1.99.10 and 1.99.9 sql server

Sorry about my mistake, I should provide the real sample for the question, my ID included characters inside:

sample code:

select ID from student order by ID

Expected output from mine          but system output
-------------------------          -----------------
JAD.1.99.9                             JAD.1.99.10
JAD.1.99.10                            JAD.1.99.9

and this ID is of nvarchar type.

like image 443
user2098512 Avatar asked May 17 '13 09:05

user2098512


People also ask

What is the meaning of order by 1/2 in SQL?

It will order the results by the first column, and then, if there are some rows with the same value in the first column it will order them by the second column.

How do I order varchar in SQL Server?

'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.

What is order by 0 in SQL?

The order statement generates a 'virtual field' that contains NULL or 0 based on the case condition. The ORDER BY is applied based on that virtual field. The zero value in the order by precedes the NULL value. That's all, it's a way to set NULLs of the field1 at the end of the order list.

Does and ordering matter SQL?

In short, no, they do not matter as the optimizer will determine the best means for fetching the data.


1 Answers

Yesterday there was a similar question where i have learned that you can use hierarchyid for version sorting(if you use at least SQL-Server 2008):

SELECT id 
FROM   student 
ORDER  BY Cast('/' + Replace(id, '.', '/') + '/' AS HIERARCHYID) 

DEMO

like image 119
Tim Schmelter Avatar answered Sep 29 '22 19:09

Tim Schmelter