0

I want to format query results in oracle and save them into an output file.

I tried this query:

spool "result.txt"
SELECT STA_CODE,DATE_CREATION,DATE_FIN_INSTANCE,DATE_FIN_TRAITEMENT  FROM DEMANDE;
spool off;

In my output file, the result looks like:

STA_CODE                  DATE_CRE DATE_FIN DATE_FIN

------------------------- -------- -------- --------

200                       09/05/17 09/05/17 09/05/17 

400                       09/05/17 09/05/17 09/05/18

I want then to write a java code that takes for each line the result and match it with name of column: for example STA_CODE=200, STA_CODE=400, DATE_CRE=09/05/17, DATE_CRE=09/05/18 .... I'm biginner in JAVA and I can't write that Bit of code. Is possible to directly format query results and then parse the output file without doing any transformation with java.

2
  • 1
    Not really sure how you want the output to look from the query. Is it all the STA_CODE values then all the DATE_CRE values and so on ? Commented May 10, 2017 at 11:13
  • Yes.. looks like cle=value Commented May 10, 2017 at 11:41

1 Answer 1

1

If you want each row separated, then use

SELECT 'STA_CODE='||STA_CODE
 ||', DATE_CRE=' ||to_date(DATE_CREATION,'DD/MM/YY')---other values 
from DEMANDE

If you want all STA_CODE first and then all DATE_CRE and then other columns in one line, separated by comma, use something like

select listagg(col1,', ') within group (order by seq)
from (
SELECT 1 as seq,'STA_CODE='||STA_CODE as col1 from DEMANDE
union
SELECT 2 as seq,'DATE_CRE='||to_date(DATE_CREATION,'DD/MM/YY') from DEMANDE
union 
---- other select queryies separated by union.

)

NOTE: You cannot guarantee order among each row. So it might happen that second STA_CODE come first and first DATE_CRE come second. To garuntee that, order by a column in all union queries.

Sign up to request clarification or add additional context in comments.

1 Comment

IT's okey..Thanks !

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.