Tim Wilcox
Tim Wilcox

Reputation: 1331

Exporting data from one table to another in SQL

This is what I am working with:

Select fields 1-24 dbo.tablename where multiple !=2;

However, in the original table, tablename, some fields are titled differently. This is a work in progress so pardon some of the inefficiencies. One example is the 6th column/field, which is titled xacct in tablename, but titled xaccount in result.

In short: Can one set up a command such as above and still account for differing field names all the while leaving the data and it's type unchanged?

Upvotes: 0

Views: 43

Answers (1)

Brian Leach
Brian Leach

Reputation: 2101

if you are doing an insert/select, column names are irrelevant, only order is important. If you are trying to rename the columns in a select statement, use the standard SQL:

SELECT field1 AS column1
     , field2 AS column2
     , field3 AS column3
     , multiple AS multiply
  FROM dbo.tablename
 WHERE multiple != 2;

Where 'FIELD1' is original column name, 'COLUMN1' is new name you are providing.

You don't have to specify new names for all of the columns, only those you are changing:

SELECT field1 AS tedd_e_bear
     , field2           
     , field3 AS spin_fidget
     , multiple AS multiply
  FROM dbo.tablename
 WHERE multiple != 2;

In the above example, field 2 still has the name field2.

Upvotes: 1

Related Questions