How to merge columns of two different tables into one table ?

  • DECLARE @String VaRCHAR(100) = 'Column1|Column2|Column3'
    DECLARE @StringColumns VaRCHAR(100) = 'EmpID|EmpName|EmpDate'
    Select 1 as Items,value as Columns1from dbo.Split('|',@String)
    Select 1 as Items,value as ColumnsTextsfrom dbo.Split('|',@StringColumns)  

    These are the scripts for two sample tables which I would like to combine in a way given in screenshot.

  • dave.chintan.m - Tuesday, August 21, 2018 7:11 AM

    DECLARE @String VaRCHAR(100) = 'Column1|Column2|Column3'
    DECLARE @StringColumns VaRCHAR(100) = 'EmpID|EmpName|EmpDate'
    Select 1 as Items,value as Columns1from dbo.Split('|',@String)
    Select 1 as Items,value as ColumnsTextsfrom dbo.Split('|',@StringColumns)  

    These are the scripts for two sample tables which I would like to combine in a way given in screenshot.

    ;WITH CTE AS
    (
      Select ROW_NUMBER() OVER(ORDER BY (SELECT 1)) as Items,value as Columns1 from dbo.Split('|',@String)
    ),
    CTE2 AS
    (
      Select ROW_NUMBER() OVER(ORDER BY (SELECT 1)) as Items,value as ColumnsTexts from dbo.Split('|',@StringColumns)  
    )
    SELECT 1 AS Items,
       CTE.Columns1,
       CTE2.ColumnsTexts
    FROM CTE
    INNER JOIN CTE2 ON CTE2.Items=CTE.Items

  • Thanks

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply