Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
scottichrosaviakosmos
Yak Posting Veteran
66 Posts |
Posted - 2010-09-09 : 03:25:22
|
have one table with following columnsid name age address salary1 12 abc $100 now i am using a query Select Case when id='' then 'id,' Else '' End + Case when name= '' then 'name,' Else '' End + Case when age= '' then 'age,' Else '' End + case when address= ''then 'Sum_Insured' else '' End + Case when salary= '' then 'salary,' Else '' End as nullcolumn from tablethis query fetch all the column which are bank for a perticular id as nullcolumnnow i want my resultset to be displayed according to id's, like:id nullcolumn1 name1 age1 address 1 ..... and so on means the above query result ie. nullcolumn values to be displayed rowwise with respect to the id.scoo |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-09 : 06:21:22
|
[code]declare @tbl as table(id int,nm varchar(10), age int, address char(10), salary int)insert into @tblselect 1,'abc', 12,'addr', 100 union allselect 2,'abc1', 13,'addr1', 200 select * from @tbldeclare @id varchar(10)select * into #temp from(select convert(varchar(10),id)id, nm,convert(varchar(10),age)age,convert(varchar(10),salary)salary from @tbl)uunpivot (col for NullColumns in(id,nm,age,salary))v outer apply(select top 1 convert(varchar(10),id)id from @tbl t where convert(varchar(10),t.id)=v.col)Tupdate #temp set @id=id=case when id is null then @id else id endselect ID,NullColumns from #tempdrop table #temp[/code]Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
|
|
|
|