Click here to Skip to main content
Click here to Skip to main content

Sort a Column Inside an Already Created Table

, 7 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip will explain how to sort a column inside a table.


This tip will explain how to sort a column inside a table.

In order to move the column, you will have to re-declare it (name and field type) and then specify the right position on the table.

Keep in mind that it is a bad practice to depend on the position columns are in a database.

This tip is here for two reasons:

  1. To help other users do this if they need it.
  2. To help me find this information again.

Using the Code

In order to make use of the code, you will have to use the SQL editing / execution environment of your database.

Let's imagine you have a table named "contents". Let's say that inside that table, you have some columns named "Spanish", "English", "German", and you would like to get them sorted alphabetically.


Spanish (text)
English (text)
German (text)


English (text)
German (text)

Spanish (text)

The SQL query you should execute to get the result shown in the previous sample is the following one:

 alter table CONTENTS change Spanish Spanish text after German

You can see in the SQL query that what you are really making is altering the table by redefining the "Spanish" column with the same name (Spanish), and type (text), placing the new "Spanish" column just after the "German" column.

This will make it...


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Joan Murt
Chief Technology Officer
Spain Spain
I'm from Catalonia (in Spain) and a specialist in CNC, PLC, robotics and automation in general.
From 1998 I've been developing software applications for special machines we make.
I'm using different technologies in each case to get the proper result, fieldbus communications, special electronics, special laser sensors, artificial vision, robot arms, CNC applications, PLC's...
You can see some of my work here:
And some videos in our YouTube channel

Comments and Discussions

GeneralUnclear PinmemberPIEBALDconsult7-Sep-12 5:32 
QuestionRe: Unclear PinmemberJoan Murt7-Sep-12 7:47 
I've made a new revision trying to explain a little bit more what it is all about, anyway, I've run out of ideas...
What do you feel it lacks? I will be more than glad to add extra explanations and details, but truly I can't imagine now what to add...
Thank you for your feedback!
[] Robots, CNC and PLC machines for grinding and polishing.

AnswerRe: Unclear PinmemberJohn Brett9-Sep-12 22:02 
GeneralRe: Unclear PinmemberJoan Murt9-Sep-12 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 7 Sep 2012
Article Copyright 2012 by Joan Murt
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid