SQL Order by string And Number

9 Flares Filament.io 9 Flares ×

Order By clause is used to order a number or string of any column of SQL.

We can esaily order any column if it;s data-type is INTEGER.Suppose we have column contain data like 2A,1,4,3B,11,111 in VARCHAR column and need to order this column.So below SQL tutorial is about Order by String and number in SQL Server & MS Access.

I have created table & inserts data using Case Statement in SQL. Now I am adding column Quality & assigning some values as under.

After executing above script,you can get below table.

You can use Update multiple rows in SQL to update multiple rows with different values in a single query.


After that,I want to sort Quality column, so I use query as we normally use.!

It gives me below result.


Now,From table you can see that row having Quality=2 comes after Quality=111.That is wrong.
As column is containing some character values,you can’t convert column to int.
So, I have used below query to sort column based on numeric & string.

For SQL Server

After executing,I got below result.!


For MS Access

We can use IIF Function for Access because it does not support Replicate Function


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">