2

I tried to get rows into column using comma delimeted using this but how to achieve this using subquery, I achived that in oracle.

SQL Server :

DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + email
FROM RDT_USER
SELECT @listStr

Oracle :

 SELECT RTRIM(XMLAGG(XMLELEMENT(E, EMAIL || ',')).EXTRACT('//text()'), ',') AS RECEIVERID 
    FROM (SELECT DISTINCT (EMAIL) AS EMAIL
          FROM RDT_USER
        )

OUTPUT Expected :

[email protected],[email protected],[email protected],[email protected]
2
  • Kindly show a sample output which u want Commented Mar 6, 2013 at 10:06
  • Thanks for reply just check question. Commented Mar 6, 2013 at 10:09

1 Answer 1

2

You can use this:

DECLARE @listStr VARCHAR(MAX) =
    STUFF(
        (
        SELECT  DISTINCT ',' + email
        FROM    RDT_USER
        FOR XML PATH(''), TYPE
        ).value('.', 'VARCHAR(MAX)')
        ,1,1,'')

SELECT @listStr

If you just want to select without variables, this should work:

SELECT 
    STUFF(
        (
        SELECT  DISTINCT ',' + email
        FROM    RDT_USER
        FOR XML PATH(''), TYPE
        ).value('.', 'VARCHAR(MAX)')
        ,1,1,'')
Sign up to request clarification or add additional context in comments.

3 Comments

bu tmy question is want to achieve this using sub query.
This approach is usually used for such tasks when using SQL Server. I don't know why you need subquery because this gets the result that you need...
you are right but am using query to execute in java environment to execute as query not as function or procedure to have declare key word and other variable

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.