1

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:

Screenshot

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.

4

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;
1
3

It's just a bit of reformatting.

SELECT 'Not a quadratic equation.' as Answer1, '' as Answer2
FROM dual
WHERE &&A = 0
UNION
SELECT to_char(-&&B/2/&A) as Answer1, '' as Answer2
FROM dual
WHERE &A != 0
  AND &B*&B-4*&A*&&C = 0
UNION
SELECT to_char((-&B+sqrt(&B*&B-4*&A*&C))/2/&A) as Answer1, 
       to_char(-&B-sqrt(&B*&B-4*&A*&C))/2/&A as Answer2
FROM dual
WHERE &A != 0
  AND &B*&B-4*&A*&C > 0
UNION
SELECT to_char(-&B/2/&A) || ',' || to_char(sqrt(-&B*&B+4*&A*&C)/2/&A) as Answer1, 
       to_char(-&B/2/&A) || ',' || to_char(-sqrt(-&B*&B+4*&A*&C)/2/&A) as Answer2
FROM dual
WHERE &A != 0
  AND &B*&B-4*&A*&C < 0;
1
  • 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. Feb 24 '16 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.