You must Sign In to post a response.
  • Category: ASP.NET

    Combine three columns into one

    I have three columns with different column name n datatable. I need to combine as one in datatable.

    My data table has
    Track_1 track_2 Track_3
    Is1 Is2 Is3

    Now my required output should be just

    Track
    Is1 Is2 Is3
  • #768130
    Hi Lily,

    First thing is if you want to make three table column in each row then there should be some matching column data in each of those tables.

    example,

    Table 1 Table2
    ---------- -------------
    EMP ID ID
    Name Salary
    Age Address

    So I can join this two table only when the Emp ID matches with the ID in the table 2.
    If that is the case you can use JOIN condition to make your requirement done.


    SELECT 'Table1'+table1.column+'Table2'+table2.column+'Table3'+table3.column
    FROM Table1
    INNER JOIN Table2 on Table1.ID = Table2.id
    LEFT JOIN/INNER JOIN Table3 on Table2.id = Table3.id // Based on your requirement

    Thanks,
    Mani

  • #768157
    This s not working as required

  • #768171
    Have you use Concact method, see below

    SELECT CONCAT(Track_1, ',',Track_2) AS Track, FROM EMP;
    //here is syntax
    SELECT SOME_OTHER_COLUMN, CONCAT(FIRSTNAME, ',', LASTNAME) AS FIRSTNAME FROM `customer`;

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768182
    Hi,

    Refer the below code..

    DataTable dtTrack; //Say dtTrack contains all the 3 columns Track_1 track_2 Track_3

    foreach (DataRow dr in dtTrack.Rows)
    {
    DataRow row;
    row = dtTrack.NewRow();
    row["Track_1"] = dr["Track_1"].ToString();
    row["Track_2"] = dr["Track_2"].ToString();
    row["Track_3"] = dr["Track_3"].ToString();
    row["Track"] = dr["Track_1"].ToString() + " " + dr["Track_2"].ToString() + " " + dr["Track_3"].ToString();
    dtTrack.Rows.Add(row);
    }



    Hope this will help you.

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #768184
    Hi

    try and let me know . Below code



    create table onetable
    (
    col1 nvarchar(100),
    col2 nvarchar(100),
    col3 nvarchar(100),
    )

    insert into onetable values('AA','BB','CC')

    select col1 + ' ' + col2 + ' ' + col2 from onetable


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #768188
    hi, we can add computed column which will be combination of all 3 columns, then would be most simplest one.
    Check the below link for reference

    http://www.c-sharpcorner.com/blogs/adding-a-calculated-column-to-a-datatable1

  • #768204
    Hai Lily,
    There could be multiple ways through which you can achieve the result as per your requirements:
    1. Manipulation at the back-end database
    -- if you want to do manipulation at the database side, you can merge the columns and make a single name for the column.
    2. Manipulation in the front end(code side)
    -- This will work once you get all the columns and then create a data-table which will have one column for these 3 and the value will be the concatenation of all three.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments