SELECT columns which contain data first I have result of a query as below first table but the requirement is to display columns first which contain data in it.

Result: 



Requirement:



Sample data generation:

Create table #CustData
(
CustID int,
CustName varchar(500),
VL1 varchar(10),
VL2 varchar(10),
VL3 varchar(10),
VL4 varchar(10),
VL5 varchar(10),
VL6 varchar(10),
NUMBER INT
)


INSERT INTO #CustData (CustID,CustName,VL1,VL2,VL3,VL4,VL5,VL6,NUMBER)
SELECT 1,'ABC', NULL, 'dl2','dl3','dl4','dl5','dl6',08586
UNION ALL
SELECT 2,'DEF', 'dl1', 'dl2',NULL,'dl4','dl5','dl6',54646
UNION ALL
SELECT 3,'GHI', 'dl1', 'dl2','','dl4',NULL,'dl6',6464
UNION ALL
SELECT 4,'JKL', NULL, 'dl2','dl3','dl4','dl5',NULL,44645
UNION ALL
SELECT 5,'MNO', 'dl1', NULL,NULL,'dl4','dl5','dl6',46457256
You could UNPIVOT, apply a ROW_NUMBER and then PIVOT it back. 

select
    CustID, 
    CustName, 
    [1] as [VL1], 
    [2] as [VL2], 
    [3] as [VL3], 
    [4] as [VL4], 
    [5] as [VL5], 
    [6] as [VL6],
    NUMBER
from(
    select 
        CustID, 
        CustName, 
        NUMBER, 
        V, 
        Seq = row_number() over (partition by CustID order by case when V = '' then 7 else null end)
    from
        (
        select 
            CustID, 
            CustName, 
            VL1, 
            VL2, 
            VL3, 
            VL4, 
            VL5, 
            VL6, 
            NUMBER
        from #CustData
        ) as cp
    unpivot
        (
        V for Vs in (VL1, VL2, VL3, VL4, VL5, VL6)
        ) up
    ) s
pivot
    (
    Max(V) for Seq in ([1],[2],[3],[4],[5],[6])
    ) p