4

I have a query:

SELECT *  FROM (SELECT col1,col2,col3,ROWNUM R 
                FROM TBL WHERE  ROWNUM < 3) 
          WHERE R='2';

(rownum will change each time giving only one record at a time )

column1      column2       column3
alpha         beta          gamma

I need result as:

alpha,beta,gamma

All i need to do is, field separators in between the output.

This is not relevant to the suggested link below and not a duplicate!

VRVigneshwara
  • 1,009
  • 8
  • 10

3 Answers3

3

If LISTAGG is not what you are looking for then, DUMMYCOL may help.--

   SELECT *
FROM
  (SELECT col1,
    ','AS DUMMYCOL,
    col2,
    ','AS DUMMYCOL,
    col3,
    ROWNUM R
  FROM TBL
  WHERE ROWNUM < 3
  )
WHERE R='2';
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
3

You can use following:-

SELECT *  
FROM (SELECT col1||','||col2||','||col3,ROWNUM R 
      FROM TBL WHERE  ROWNUM < 3) 
WHERE R='2';
Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40
3

There are two ways:

CONCAT function

SELECT *
FROM
  (SELECT concat(concat(concat(concat('a', ','), col2), ','), col3),
    ROWNUM R
  FROM TBL
  WHERE ROWNUM < 3
  )
WHERE R=2;

|| concatenation operator

SELECT *
FROM
  (SELECT col1||','||col2||','||col3,ROWNUM R FROM TBL WHERE ROWNUM < 3
  )
WHERE R=2;

Another thing, no need to use single quotes around the value for ROWNUM, it unnecessarily converts it into string, just leave it as NUMBER.

Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124