0

How can i Convert

Id  Name TypeCode   UseType OptionsText
1    A     AA          1      Test1
2    B     AB          2      Test2
3    B     AB          2      Test3

To this

Id  Name TypeCode   UseType OptionsText
1    A     AA          1      Test1
2    B     AB          2      Test2;;Test3

I am trying to do it with coallace but not able to make it work properly.

5
  • 2
    which database are you using?
    – T I
    Dec 30, 2013 at 13:30
  • SQL is the language that many different database vendors implement, along with their own custom extensions. Do you know which database PRODUCT (MySQL, SQL Server, Oracle) you are using? Dec 30, 2013 at 13:32
  • Microsoft SQL Server 2005, 2008? oracle 11g? mySQL? vendor and version help. Coalesce doesn't do this. in Oracle it's WM_Concat or ListAgg.)
    – xQbert
    Dec 30, 2013 at 13:32
  • @all i amm using SQL Server Dec 30, 2013 at 13:33
  • This gives you an example of how to accomplish your desired results. stackoverflow.com/questions/12559551/…
    – xQbert
    Dec 30, 2013 at 13:34

1 Answer 1

2

You can use the FOR XML to do so:

SELECT 
  t1.Name,
  t1.TypeCode,
  t1.UseType ,
  STUFF((
    SELECT ';; ' + t2.OptionsText
    FROM Tablename t2
    WHERE t2.Name   = t1.Name
      AND t1.TypeCode = t2.TypeCode
    FOR XML PATH (''))
  ,1,2,'') AS OptionsText
FROM Tablename t1
GROUP BY t1.Name,
      t1.TypeCode,
      t1.UseType; 

This will give you:

| NAME | TYPECODE | USETYPE |    OPTIONSTEXT |
|------|----------|---------|----------------|
|    A |       AA |       1 |          Test1 |
|    B |       AB |       2 |  Test2;; Test3 |
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.