Results 1 to 3 of 3

Thread: SQL Statement

  1. #1
    Join Date
    Feb 2004
    Posts
    2

    SQL Statement

    Hi,

    I need to write a query for the problem defined below. Can anyone help me ?

    Input table
    T1

    Emp NO Emp Name Depedent Sex
    312 a z m
    312 a y m
    312 a x f
    423 b k m
    423 b j f
    234 c l m

    out put should be printed as

    Emp no Emp name Dependent
    312 a z,y,x
    423 b k,j
    234 c l

    Please give me query to print the output specified as above.

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    Create FUNCTION dbo.fn_Concat (@col varchar(100))
    returns varchar(1000)
    as
    begin
    declare @x varchar(1000)
    set @x=''
    select @x=@x+','+convert(varchar,Depedent ) from T1 where ltrim(rtrim(Empname)) = ltrim(rtrim(@col))
    set @x=substring(@x,2,len(@x))
    return (@x)
    end
    go

    create table T1 (EmpNO int,EmpName varchar(10),Depedent varchar(10), Sex varchar(1))
    insert into T1 select 312, 'a', 'z','m'
    insert into T1 select 312, 'a', 'y','m'
    insert into T1 select 312, 'a', 'x','f'
    insert into T1 select 423, 'b', 'k','m'
    insert into T1 select 423, 'b', 'j','f'
    insert into T1 select 234, 'c', 'l','m'
    go
    Select EmpNO,EmpName, dbo.fn_Concat(EmpName) as Depedent from T1 group by Empno,EmpName
    go

  3. #3
    Join Date
    Feb 2004
    Posts
    2

    Lightbulb

    Hi,
    Thanks for your reply. It was very helpful.
    Regards
    kannan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •