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? Commented 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? Commented 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.) Commented Dec 30, 2013 at 13:32
  • @all i amm using SQL Server Commented Dec 30, 2013 at 13:33
  • This gives you an example of how to accomplish your desired results. stackoverflow.com/questions/12559551/… Commented 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 |
Sign up to request clarification or add additional context in comments.

4 Comments

thanks its working great but its actually cutting some text from text
Its cutting the first words of the text
@Rex - Can you please update this demo with the values you are working on to see the issue you have.
Replaced FOR XML PATH ('')) ,1,2,'') with this FOR XML PATH ('')) ,1,1,'') now working fine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.