views:

209

answers:

5

I've this GIGANTIC view with several hundred columns and I need to select 114th column something like:

SELECT "144" FROM MyView;

PS: Obviously, I don't know the name of the column. I just copied the results row into an Excel file, searched for a particular value that is in EJ column so I want to select all rows showing only nth column in my view to do my further debugging.

Cheers!

+1  A: 

I guess the answer to this problem is highly db vendor dependent.

René Nyffenegger
Its SQL Sever 2005
Vishal Seth
A: 

No, you can't select by index using either standard SQL or Oracle.

Other databases vendors may have their own proprietary command for doing this, however.

Dolph
+1  A: 

You could use the following in a generic stored procedure, where you pass in the column name, table name, and the value of n. This will work on MSSQL 2000 - 2008.

CREATE PROC usp_select_Nth ( @table_name sysname, @column_name sysname, @nth int ) AS BEGIN

SET @exec = 'SELECT MAX(' + @column_name + ') from ' + @table_name + ' WHERE ' + @column_name + ' NOT IN ( SELECT TOP ' + LTRIM(STR(@nth - 1)) + ' ' + @column_name + ' FROM ' + @table_name + ' ORDER BY ' + @column_name + ' DESC )' EXEC (@exec)

END

Ben.Vineyard
Thanks Dolph. I always forget about the code snippet functionality for posting.
Ben.Vineyard
The whole point is that I don't know column name. thanks anyways
Vishal Seth
I missed that point. :(
Ben.Vineyard
+1  A: 

I'd suggest giving all the columns names based upon their Excel column name. There should be an excel solution to make the first row equal to the column name (ie AA, BB, etc). Then do your import, then select based upon the column name which should be computable.

MindStalker
+2  A: 

If you are using MS SQL Server you can

sp_help ViewName

and then scroll to the 144th column in the Column_name result set to see what the name of that column is.

Additionally you can choose "Copy with Headers" in the result pane in SQL Server Management Studio and paste the result set into Excel with the headers (column names) intact.

cfeduke
Thanks, this serves my purpose. I'll accept that answers as soon as it allows me to do so (in 5 mins).
Vishal Seth