sqlgridviewfieldcellselectcommand

SELECT statement issue - Joining of data of different fields in one cell of Gridview


I am using following SELECT statement for Gridview:

  SelectCommand="SELECT ID, Date, Train, I_R, Dir_Ind, Detn, Rly, DiV, Loco, Shed, locoClass, loco_type,
(maj_sch_type + ','+ ' ' + maj_sch_place +',' +' '+ (CAST(maj_sch_dt as VARCHAR(11)) + ' '+'/' 
+ ' ' + min_sch_type +',' + ' ' + min_sch_place + ',' + ' '+(CAST(min_sch_dt as VARCHAR   (11))) ))    as "major",
 Equipt, I_R, reason 
 FROM PunctualityMain Order by Date ASC"

With the above, I am getting results in single cell from 6 (3 + 3) different fields of SQL data as

 POH, KPA, Jan 2 2012 / IB, Shed, Apr 18 2012

Issue is this that whenever data in any group of 3 fields (before or after '/') is blank, Gridview cell is remain completely blank. If both group of fields having data than Gridview dispaly is OK as mentioned above. Can SELECT statement be modified for showing data in Gridview with any one group of fields are empty?


Solution

  • If you try to concatenate a null string with a non-null string, the result will be null.

    Select NULL + 'test' 
    

    this will return NULL.

    Also, I think your code didn't copy paste extremely well into your question, but you need to use the ISNULL() function. I think you can get the point. You'll probably have to clean up the code a bit for it to run. Just wrap every field that you are concatenating with ISNULL(MyField, '')

    SELECT 
        ID, 
        Date, 
        Train, 
        I_R, 
        Dir_Ind, 
        Detn, 
        Rly,    
        DiV, 
        Loco, 
        Shed, 
        locoClass, 
        loco_type,
        (isnull(maj_sch_type, '') + ', ' + isnull(maj_sch_place, '') +', '+ (isnull(CAST(maj_sch_dt as VARCHAR(11), '')) + ' / ' + isnull(min_sch_type, '') +', ' + isnull(min_sch_place, '') + ', '+ (isnull(CAST(min_sch_dt as VARCHAR   (11)), '')) ))    as "major",
         Equipt, 
        I_R, 
        reason 
     FROM PunctualityMain 
     Order by Date ASC
    

    Reference