# How to reformat Oracle SQL quadratic-equation solver script?

I was wondering how I might be able to tweak this SQL script:

``````ALTER SESSION
SET NLS_NUMERIC_CHARACTERS='. ';

SELECT 'Not a quadratic equation.' ans
FROM dual
WHERE &&A = 0
UNION
SELECT 'x = ' || to_char(-&&B/2/&A)
FROM dual
WHERE &A != 0
AND &B*&B-4*&A*&&C = 0
UNION
SELECT 'x1 = ' || to_char((-&B+sqrt(&B*&B-4*&A*&C))/2/&A) || ', x2 = ' || to_char(-&B-sqrt(&B*&B-4*&A*&C))/2/&A
FROM dual
WHERE &A != 0
AND &B*&B-4*&A*&C > 0
UNION
SELECT 'x1 = (' || to_char(-&B/2/&A) || ',' || to_char(sqrt(-&B*&B+4*&A*&C)/2/&A) || '), ' || 'x2 = (' || to_char(-&B/2/&A) || ',' || to_char(-sqrt(-&B*&B+4*&A*&C)/2/&A) || ')'
FROM dual
WHERE &A != 0
AND &B*&B-4*&A*&C < 0;
``````

to treat the quadratic equation solver-output from the inputs (a=1, b=5, c=1) displayed in the below image: as separate values contained within separate columns denoted "Answer 1" and "Answer 2" respectively; so "Answer 1" column contains -2.0871... and "Answer 2" column contains -4.791...

I took a few hacks at it but I couldn't quite format according to the aforementioned.

Well i got the following to get the output in two columns:

``````SELECT 'Not a quadratic equation.' as "ans1", '0' as "ans2"
FROM dual
WHERE &&A = 0
UNION
SELECT 'x = ' || to_char(-&&B/2/&A) as "ans1", '0' as "ans2"
FROM dual
WHERE &A != 0
AND &B*&B-4*&A*&&C = 0
UNION
SELECT 'x1 = ' || to_char((-&B+sqrt(&B*&B-4*&A*&C))/2/&A) as "ans1", 'x2 = ' || to_char(-&B-sqrt(&B*&B-4*&A*&C))/2/&A as "ans2"
FROM dual
WHERE &A != 0
AND &B*&B-4*&A*&C > 0
UNION
SELECT 'x1 = (' || to_char(-&B/2/&A) || ',' || to_char(sqrt(-&B*&B+4*&A*&C)/2/&A) || '), ' as "ans1", 'x2 = (' || to_char(-&B/2/&A) || ',' || to_char(-sqrt(-&B*&B+4*&A*&C)/2/&A) || ')' as "ans2"
FROM dual
WHERE &A != 0
AND &B*&B-4*&A*&C < 0;
``````

It's just a bit of reformatting.

``````SELECT 'Not a quadratic equation.' as Answer1, '' as Answer2
FROM dual
WHERE &&A = 0
UNION
FROM dual
WHERE &A != 0
AND &B*&B-4*&A*&&C = 0
UNION
• Hi @Phil! I tried your submissions but the following was returned: `ORA-01790: expression must have same datatype as corresponding expression 01790. 00000 - "expression must have same datatype as corresponding expression" *Cause: *Action: Error at Line: 11 Column: 37` tried wrap the AS column aliases within quotes but that was unsuccessful. – alexanderjsingleton Feb 24 '16 at 16:16