1

I am wondering if there is a way to select "like". For example i have columns h1, h2, h3 i want my select to pick which column to grab based on user input.

Select like "user var will be 1,2 or 3" from table

Can you have a varying select or will i have to do Select * from table and then filter my data table?

Another though I had was through having it add the h onto the users var in C#

Select 'h" + var + " ' from table

Himarm
  • 163
  • 1
  • 9

3 Answers3

1

You could build the query on the fly:

int col = Convert.ToInt32(request["usercolumn"]);
string q = "SELECT h" + col + " FROM table";

My c# is a bit weak, but the gist here is take the user input, make sure it's an integer and build your query using the user input.

Robbert
  • 6,481
  • 5
  • 35
  • 61
  • This is what i waslooking to do i had ' ' around it and it was failing lol, so i was looking for otherways, took the extra ' ' off and works great thanks. –  Dec 08 '14 at 20:25
0

SELECT * is typically bad practice. If you did something like the data table filtering I would specify the possible columns explicitly. I would also look at using dynamic SQL. Dynamically choose column in SQL query

Community
  • 1
  • 1
db_brad
  • 903
  • 6
  • 22
0

Robbert code will work but if use will enter greater than 3 then that will give error, better to use case like this

DECLARE @col int  = 1
select case @col when 1 then h1
                 When 2 then h2
                 Else h3 END
From table 
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85