Monday, March 19, 2012

Is there WHERE for columns

Hi everybody,
Is there a way to filter columns at runtime based on their name?

I create SELECT statement which includes all columns, then, if I get some parameter I leave some columns out and don't select data from them.

Is it possible?

I'm working with MS SQL 2000.You can't omit columns. The closest thing you can do is use CASE expressions to hide the data you don't want to show, such as:

SELECT Col1, Col2, CASE WHEN Col1 = 'someValue' THEN Col3 ELSE NULL END AS Col3
FROM YourTable|||Is this possible:

SELECT Col1, Col2, CASE WHEN SomethingOutsideThisSelectCommand = 'someValue' THEN Col3 ELSE NULL END AS Col3
FROM YourTable|||As long as it's a valid SQL expression, it shouldn't be a problem.

No comments:

Post a Comment