You must Sign In to post a response.
  • Category: SQL Server

    Need the sql query in MS SQL Server

    Hi ,
    I have a table with two fields:FName and LName with following data.
    FName LName
    A B
    A B
    C D
    E F
    I need a query which will get me the following output
    FName LName FullName
    A B AB
    A B AB
    C D
    E F
    For the duplicate record, full name field should have concatenated values.
    I applied the group by query to get only one of the two repeated records.But both repeated records should come in resultset.I tried the following:
    select FName,LName,
    when count(*) > 1 then FName+LName
    else ' '
    end as 'FullName'
    from <tablename> group by FName,LName
  • #769371
    your Requirement is not clear. let us know more in detail

  • #769421

    Use the below SQL Script to get the concatenated values for the duplicate record

    DECLARE @tablename TABLE (
    FName char(1),
    LName char(1)

    INSERT INTO @tablename (FName, LName)
    VALUES ('A', 'B')
    INSERT INTO @tablename (FName, LName)
    VALUES ('A', 'B')
    INSERT INTO @tablename (FName, LName)
    VALUES ('C', 'D')
    INSERT INTO @tablename (FName, LName)
    VALUES ('E', 'F')

    WHEN t2.[count] > 1 THEN t1.FName + t1.LName
    END FullName
    FROM @tablename t1
    COUNT(*) AS [count]
    FROM @tablename
    GROUP BY FName,
    LName) t2
    ON t1.FName = t2.FName
    AND t1.LName = t2.LName

    Please mark this as Answer, if this helps


  • Sign In to post your comments