How to hide Columns with Null Values using SQL server 2005

Hi Guys,

I have any below query how to hide the column if record has null value.

Eg
select CalendarID, Budget, Cost from Calendar_Financial

CalendarID Budget Cost

1                 1000    Null

Open in new window


Expected Result "Basically when the cost value is null i want to hide only Cost column"

CalendarID Budget

1                 1000

Open in new window



Thanks in Advance
Shabbir
sportcalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Guessing that's not possible to exclude an entire column based on whether or not all values inside it are NULL, which means you'll have to write an IF block to test, and have two different statements...

Declare @a int = 5, @b int = 7, @str varchar(10) = NULL

IF @str IS NULL
	SELECT @a, @b
ELSE
	SELECT @a, @b, @str

Open in new window

0
sportcalAuthor Commented:
Thanks Jim for your reply i don't think it will work out. If anybody else have an alternative solution please do share.

Thanks,
Shabbir
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Good luck looking, but I have dibs on 'you can't do that' as the correct answer here.

Out of curiosity, what would be the negative impact of having a column with a NULL value in it?   Seems like it would be far easier to handle the NULL downstream then to conditionally include/exclude the column.  

Not to mention that if this SQL is the source of something else that requires the schema to always be the same, such as an SSRS report or SSIS data flow, it could cause those to fail when the schema changes.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sportcalAuthor Commented:
Hello Guys,

I have found temporary solution to hide the column with null values, First thing i have copied my orginal table to the temp table and create below procedure using this procedure will delete all the field which has null values, So would like to share everyone so that it might help.

GO
/****** Object:  StoredProcedure [dbo].[mysp_DropEmptyColumns]    Script Date: 09/05/2013 10:47:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[mysp_DropEmptyColumns]
  @tableName nvarchar(max)
as begin
  declare @FieldName nvarchar(max)
  declare @SQL nvarchar(max)
  declare @CountDef nvarchar(max)
  declare @FieldCount int

  declare fieldNames cursor  local fast_forward for
    select c.name
      from syscolumns c
        inner join sysobjects o on c.id=o.id
      where o.xtype='U'
        and o.Name=@tableName

  open fieldNames
  fetch next from fieldNames into @FieldName
  while (@@fetch_status=0)
  begin
    set @SQL=N'select @Count=count(*) from "'+@TableName+'" where "'+@FieldName+'" is not null'
    SET @CountDef = N'@Count int output';
    exec sp_executeSQL @SQL, @CountDef, @Count = @FieldCount output
    if (@FieldCount=0)
    begin
      set @SQL = 'alter table '+@TableName+' drop column '+@FieldName
      exec sp_executeSQL @SQL
      print @SQL
    end
    fetch next from fieldNames into @FieldName
  end

  close fieldNames
end

Open in new window


Thanks everyone,

Shabbir
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Looks pretty slick.  Good luck with your project.  -Jim
0
sportcalAuthor Commented:
Thanks jim for your help

Regards,
Shabbir
0
sportcalAuthor Commented:
I have found an Alternative solution and thanks everyone who have shared the knowledge.

Thanks,
shabbir
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.