Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I supress an sql column from displaying conditionally?

Status
Not open for further replies.

Freddo1

MIS
Sep 28, 2011
1
AU
I need to have an sql statement which sometimes includes a column and other times excludes ie IncludeIf and excludeif.

The equivalent of Oracle SQL Plus column noprint. Is there a way to do this in a Transact-sql statement?

Thanks
 
You would have to use a dynamic query to do this. eg:
Code:
DECLARE @SQL nvarchar(MAX)

SET @SQL = 'SELECT Col1, Col2'
/* include Col3 only on the 15th of each month. */
SET @SQL = @SQL +(CASE WHEN DATEPART(d, GETDATE()) = 15 THEN ', Col3' ELSE '' END) 
SET @SQL = @SQL + ' FROM Tab1'

EXEC sp_executesql @SQL
 
no need for dynamic SQl

use

Select col1,col2,case when CASE WHEN DATEPART(d, GETDATE()) = 15 then col3 else null end as col3conditionally
from tablename
 
Thats not the same as it outputs a column and only makes the content null based on a condition.

Oracle noprint and starskys solution don't output the column at all, which is what the op wants.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top