473,656 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing the position of column by writing a query

8 New Member
is there any way to change the position of a column in a table by writing sql query?

for eg;
Table 'demo' contains 3 fields:
companyName
EmpName
id

I need to move the 'id' from last position to first by writing a query along with the data..

can anybody help me pls...
May 26 '08 #1
2 9576
deepuv04
227 Recognized Expert New Member
is there any way to change the position of a column in a table by writing sql query?

for eg;
Table 'demo' contains 3 fields:
companyName
EmpName
id

I need to move the 'id' from last position to first by writing a query along with the data..

can anybody help me pls...
Hi,
You can do in two ways.
1. ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE
then update the table as
update Table_Name set New_Column = Old_Column
then delete the old column
Alter table Table_Name drop column Old_Column
( Create new columns so that they must be in the order you want)

2. Create a new table as
SELECT Id,CompanyName, EmpName into New_Table From Table_Name
then drop the old table
then rename the original table


why do you want to? As per relational database theory the order of the
columns in a table is not important. If you want to see the columns in certain order specify the column names in some specific order in select query

thanks
May 26 '08 #2
ck9663
2,878 Recognized Expert Specialist
Hi,
You can do in two ways.
1. ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE
then update the table as
update Table_Name set New_Column = Old_Column
then delete the old column
Alter table Table_Name drop column Old_Column
( Create new columns so that they must be in the order you want)

2. Create a new table as
SELECT Id,CompanyName, EmpName into New_Table From Table_Name
then drop the old table
then rename the original table


why do you want to? As per relational database theory the order of the
columns in a table is not important. If you want to see the columns in certain order specify the column names in some specific order in select query

thanks

I would agree with you.

Mary, If you need this columns re-arrange so that you can BULK INSERT into it, I would suggest you just create a temp table that you can use for that and check the quality of the records that you will be inserting to your table. This way your don't just insert data without knowing it's quality.

If this is for display purposes, just do something l ike:

Expand|Select|Wrap|Line Numbers
  1. select id. companyName, EmpName from YourTable
May 26 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
9568
by: Doug Baroter | last post by:
What is a good method/mechanism to swap the position of multiple columns? For instance, tblXZY has the followings columns and respective positions: tblXZY ====== xyzUUID 1 fn 2 ln 3 phone 4
1
6494
by: iporter | last post by:
Hi - I am changing a field from type nvarchar to type text, given that I need to store strings longer than 255 characters. To do this I change the data type in SQL Server, then I change the parameter code in the calling procedure, as per below: cmd.Parameters.Append(cmd.CreateParameter("@title", adVarWChar, adParamInput, 255, title)); becomes:
6
4593
by: Brett Maton | last post by:
Hi NG, How do I find out the size of a column ? I am retrieving large objects from the pg_largeobject table and creating a files, I would like to know the length of the data column so that I can implement buffering instead of writing hundreds of small chunks to disk. Any help would be appreciated.
20
2755
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into normalized data. The task is made more difficult by the fact that the structure itself can vary from month to month (in well defined ways). So, I used the SQL-centric approach, taking vertical stripes at a time so that, for instance, for each...
1
8833
by: Vinny Vinn | last post by:
I would like to insert a new column in DataTable at a specified position at runtime. Unfortunatly, the DataColumnCollection does not expose any method to do this (like AddAt),Any ideas would be greatly appreciated? Vinny
0
930
by: Gary | last post by:
Hi all, By default when we create a Template Column, the position of the Template Column will be 1. Is it possible to change its position at Design Time or Runtime i.e if I want a Template Column to be at no. 2, then how can we do this?
4
2975
by: Scot L. Harris | last post by:
Currently using Postgresql 7.2.4-5.80 with php 4.2.2.-8.0.8 on a redhat 8.0 system. I am writing some php scripts where I want to generate a list of the column names in a particular table that the user selects. I could take the brute force method and hard code the column names but then every time I add a new table or modify an existing one I would have to modify the code. What I want is to have a generic function that given the table...
3
4677
by: Aziz | last post by:
1. I have a shopping basket DataGrid with a list of products. What I want to do is when the user clicks on a row, a button will become visible/be created that allows user to edit the quantity. The button will be dynamic and always show up on the same Y-axis position as the selected row. So, how do I get back the X, Y co-ordinates of a specific row in a datagrid relative to the form? 2. I managed to change individual column width using:
4
4671
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through and write the fields into strings of rows, I want to be able to put field1 in row1/column position1, field2 in row1/column position10,.....etc. I've included the basic code to write a string of text. I understand the process of how to write the...
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.