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

SqlSql Server

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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 

Solution 2 - Sql

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.

Solution 3 - Sql

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

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select#LanguageManualSelect-REGEXColumnSpecification

Solution 4 - Sql

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)

Solution 5 - Sql

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

Solution 6 - Sql

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 : https://stackoverflow.com/questions/9122/select-all-columns-except-one-in-mysql

Solution 7 - Sql

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.

Solution 8 - Sql

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')

Solution 9 - Sql

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');

Solution 10 - Sql

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;

Solution 11 - Sql

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.

Solution 12 - Sql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGiri PrasadView Question on Stackoverflow
Solution 1 - SqlRahul TripathiView Answer on Stackoverflow
Solution 2 - SqlTommCattView Answer on Stackoverflow
Solution 3 - SqlFatoreView Answer on Stackoverflow
Solution 4 - Sqlps_prakash02View Answer on Stackoverflow
Solution 5 - SqlAshish GuptaView Answer on Stackoverflow
Solution 6 - SqlSagar JoonView Answer on Stackoverflow
Solution 7 - SqlVlad BezdenView Answer on Stackoverflow
Solution 8 - SqlBikash MahatoView Answer on Stackoverflow
Solution 9 - SqlSergio PisoniView Answer on Stackoverflow
Solution 10 - SqlOmid NasriView Answer on Stackoverflow
Solution 11 - SqlAbhinav VanamView Answer on Stackoverflow
Solution 12 - Sqlyasir kkView Answer on Stackoverflow