96

How to select all the columns of a table except one column?

I have nearly 259 columns I cant mention 258 columns in SELECT statement.

Is there any other way to do it?

7
  • 5
    Which DBMS you are using? Mar 17, 2015 at 9:19
  • 12
    You've already got a table with 259 columns. Why do you care about selecting one less? You've already made a monster of a database and a monster of a result set, one column won't make a difference.
    – Luaan
    Mar 17, 2015 at 9:24
  • No, that's not possible. You have to list every column (or write a function that does this for your). But many SQL clients can help you to "generate" that list automatically through auto completion. Plus which DBMS are you using? Postgres? Oracle? Mar 17, 2015 at 9:27
  • 1
    @GiriPrasad In Management studio, you can right click the table, select Script table as -> Select to -> New query window. This will generate a select with all the columns, and you can just get rid of the one you don't want.
    – Luaan
    Mar 17, 2015 at 13:15
  • 9
    possible duplicate of SQL exclude a column using SELECT * [except columnA] FROM tableA?
    – Bridge
    Mar 24, 2015 at 15:03

12 Answers 12

77

You can use this approach to get the data from all the columns except one:-

  1. Insert all the data into a temporary table
  2. Then drop the column which you dont want from the temporary table
  3. Fetch the data from the temporary table(This will not contain the data of the removed column)
  4. Drop the temporary table

Something like this:

SELECT * INTO #TemporaryTable FROM YourTableName

ALTER TABLE #TemporaryTable DROP COLUMN Columnwhichyouwanttoremove

SELECT * FROM #TemporaryTable 

DROP TABLE #TemporaryTable 
9
  • 1
    How do you know which DBMS Giri is using? Mar 17, 2015 at 9:27
  • 16
    @a_horse_with_no_name:- I dont know thats why I said its just an approach! Mar 17, 2015 at 9:28
  • 7
    I must admit, this is a novel solution. However, this can place quite a load on a busy server every time the query needs to be executed. And all because one db developer has an attack of the lazies. "I can't mention 258 columns in select statement" indeed!
    – TommCatt
    Mar 18, 2015 at 4:55
  • 15
    In fact, in thinking about it some more, I think I would have a long sit-down with any programmer who executed such code. Of course, I would also like to have a long discussion with whoever designed such a table and then again with the analyst who requested such a query. Heads will roll, people!
    – TommCatt
    Mar 18, 2015 at 4:59
  • 2
    How can it be an accepted answer ? I'm waiting to see a guy who ask why this query is so long with tables of billions of row... @TommCatt, I would sit next to you. Oct 7, 2020 at 13:14
19

Create a view. Yes, in the view creation statement, you will have to list each...and...every...field...by...name.

Once.

Then just select * from viewname after that.

1
16

This is not a generic solution, but some databases allow you to use regular expressions to specify the columns.

For instance, in the case of Hive, the following query selects all columns except ds and hr:

SELECT `(ds|hr)?+.+` FROM sales
3
  • 2
    I was a serious hive developer and did not know about this - v cool. Dec 20, 2019 at 20:29
  • This regex is flawed. If you want to exclude two columns day and day_hour, (day|day_hour)?+.+ will still match day_hour column. That's because regex engine is eager on |. Although changing the order to (day_hour|day)?+.+ can solve this issue, the better method is using negative lookahead, (?!(day|day_hour)$).+.
    – lovetl2002
    Jun 28, 2020 at 6:47
  • I am using Hive but somehow it's giving me an error saying Invalid table alias or column reference. It seems like it's reading "(colname)?+.+" as a literal column name. Any solution for this? Thanks.
    – Bowen Liu
    Sep 14, 2020 at 23:54
12

You can get the column name details from sys.columns table

Try the following query:

SELECT * FROM SYS.COLUMNS 
WHERE object_id = OBJECT_ID('dbo.TableName') 
AND [Name] <> 'ColumnName'

DECLARE @sql as VARCHAR(8000)
SET @sql = 'SELECT '

SELECT @sql += [Name] + ', ' FROM SYS.COLUMNS 
WHERE object_id = OBJECT_ID('dbo.TableName') 
AND [Name] <> 'ColumnName'

SELECT @sql += ' FROM Dbo.TableName'

EXEC(@sql)
9

I just wanted to echo @Luann's comment as I use this approach always.

Just right click on the table > Script table as > Select to > New Query window.

You will see the select query. Just take out the column you want to exclude and you have your preferred select query. enter image description here

1
  • Simple solution I also ended up with - even simpler is using the "Select top 1000 Rows" though. :) Jan 29, 2020 at 12:27
7

There are lot of options available , one of them is :

 CREATE TEMPORARY TABLE temp_tb SELECT * FROM orig_tb;
 ALTER TABLE temp_tb DROP col_x;
 SELECT * FROM temp_tb;

Here the col_x is the column which u dont want to include in select statement.

Take a look at this question : Select all columns except one in MySQL?

1
  • Thanks for the response Mar 18, 2015 at 5:11
7

You can retrieve the list of column name by simple query and then remove those column by apply where query like this.

SELECT * FROM (
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'TableName'
) AS allColumns
WHERE allColumns.COLUMN_NAME NOT IN ('unwantedCol1', 'unwantedCol2')
1
  • 2
    It can be done with a single where clause, no need to select from select
    – astentx
    Oct 29, 2020 at 10:58
6

If you are using DataGrip you can do the following:

  1. Enter your SELECT statement SELECT * FROM <your_table>;
  2. Put your cursor over * and press Alt+Enter
  3. You will get pop up menu with Expand column list option
  4. Click on it and it will convert * with full list of columns
  5. Now you can remove columns that you don't need

Here is a link for an example on how to do it.

1
  • this is a great solution Jan 19, 2022 at 14:15
4

Without creating new table you can do simply (e.g with mysqli):

  1. get all columns
  2. loop through all columns and remove wich you want
  3. make your query

$r = mysqli_query('SELECT column_name FROM information_schema.columns WHERE table_name = table_to_query');

$c = count($r); while($c--) if($r[$c]['column_name'] != 'column_to_remove_from_query') $a[] = $r[$c]['column_name']; else unset($r[$c]);

$r = mysqli_query('SELECT ' . implode(',', $a) . ' FROM table_to_query');
1

Try the following query:

DECLARE @Temp NVARCHAR(MAX); 
DECLARE @SQL NVARCHAR(MAX);

SET @Temp = '';
SELECT @Temp = @Temp + COLUMN_NAME + ', ' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='Person' AND COLUMN_NAME NOT IN ('Id')  

SET @SQL = 'SELECT ' + SUBSTRING(@Temp, 0, LEN(@Temp)) +' FROM [Person]';
EXECUTE SP_EXECUTESQL @SQL;
0

In your case, expand columns of that database in the object explorer. Drag the columns in to the query area.

And then just delete one or two columns which you don't want and then run it. I'm open to any suggestions easier than this.

-1

Only one way to achieve this giving column name. There is no other method found. You must have to list all column name

1
  • 1
    While what you say may be the case in general there are multiple answers describing [corner] cases where the intended result can be achieved. Dec 20, 2019 at 20:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.