Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
SQL
DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(StuDocname)
                    from StudentDocuments
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'')

set @query = 'SELECT StudentCode,Stuname, ' + @cols + ' from
             (
                select DISTINCT SC.StuCurrentId,StudentCode,upper(First_Name + '' '' + Middle_Name + '' '' + Last_Name) as Stuname
                ,Sd.StuDocname,SASD.IsSelected,(case when IsSelected = 1 then ''YES''
                 when IsSelected = 0 then ''NO''
            end) as status
 from tbl_Student_Current SC
inner join StudentAdmission SA on SC.StudentId=SA.StudentId
inner join StudentAdmission_StudentDocuments SASD on SA.StudentId=SASD.StudentId
left outer join StudentDocuments SD on SASD.StudentDocId=SD.StuDocId

            ) x
            pivot
            (
                MAX(status)
                for StuDocname in (' + @cols + ')
            ) p '

execute(@query)
Posted
Comments
Harsh Athalye 22-Dec-14 23:41pm    
Can you please describe in detail exactly where do you want to remove null values in the query? ISNULL() or COALESCE() are your friends, in either case.
krishna97 23-Dec-14 0:16am    
ID Stuname abc xyz abc1
1 kkp yes Null Null
2 kkp1 null null null


1 solution

Just set the the value which you need if there is Null

like below

SQL
Declare @var as varchar(10)=null
select isnull(@var,0) as Result
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900