I got the Solution
Go
CREATE TABLE TEMP (id int ,C1 INT, C2 INT, C3 INT,C4 INT,C5 INT, C6 INT)
Go
INSERT INTO TEMP (id,C1,C3,C5) VALUES (1,2,3,5)
INSERT INTO TEMP (id,C1,C3,C5) VALUES (1,6,7,8)
Select * from TEMP
Go
DECLARE @name VARCHAR(100)
DECLARE @SQL1 NVARCHAR(max)
DECLARE @sqlCommand NVARCHAR(max)
DECLARE @OUPUT VARCHAR(max)
Declare @flg int set @flg=0
SET @OUPUT =''
DECLARE db_cursor CURSOR FOR
Select name from sys.syscolumns where id in (Select id from sys.sysobjects where name='TEMP ')
OPEN db_cursor FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlCommand = 'SELECT @flg=1 from TEMP where LEN( '+@name+')' + '> 0 '
--print @sqlCommand
--print @flg
EXECUTE sp_executesql @sqlCommand, N'@name nvarchar(100),@flg int =0 OUTPUT', @name = @name,@flg=@flg OUTPUT
IF @flg=1 SET @OUPUT =@OUPUT+@name+', '
set @flg=0
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
SET @SQL1='SELECT '+LEFT(@OUPUT,LEN(@OUPUT)-1)+ ' FROM TEMP '
EXEC SP_EXECUTESQL @SQL1
GO
Drop TABLE TEMP
Assuming that there will not be any change in the data for the ID
say for id=1 the values will be present only C1,C3,C5 rest of the columns will be null for whole id=1
where as for id=2 the values may present in C4,C8,C9 rest of the columns will be null for whole id=2
Thanks for your Time!