I often find myself teaching mathematics to my daughter, and recently, she has been diving into the realm of Quadratic Equations. As we delved deeper into the subject, we realized that solving quadratic equations by hand and verifying the solutions can be quite cumbersome and time-consuming. Determined to streamline this process, I embarked on a journey to leverage the power of T-SQL for solving quadratic equations. Today, I am excited to share my learnings with all of you. In this blog post, we will explore a practical approach to solving quadratic equations using T-SQL. Let’s dive in!

### Understanding Quadratic Equations

A quadratic equation is typically represented as **ax^2 + bx + c = 0**, where ‘a’, ‘b’, and ‘c’ are constants, and ‘x’ represents the unknown variable. The goal is to find the values of ‘x’ that satisfy the equation. To solve the quadratic equation, we can use the quadratic formula.

The quadratic formula states that the solutions to the quadratic equation ax^2 + bx + c = 0 can be calculated using the following formula: **x = (-b ± √(b^2 – 4ac)) / (2a)**

To solve quadratic equations using T-SQL, we can translate the quadratic formula directly into a SQL query. For example, if I have this Equation: **x^2 – 5x + 6 = 0**:

I can write T-SQL as follows:

DECLARE @a FLOAT, @b FLOAT, @c FLOAT; SET @a = 2; SET @b = -5; SET @c = 3; SELECT x1 = (-@b + SQRT(POWER(@b, 2) - (4 * @a * @c))) / (2 * @a), x2 = (-@b - SQRT(POWER(@b, 2) - (4 * @a * @c))) / (2 * @a);

The above will give a solution as **x1 = 1.5** and **x2 = 1**.

While working with my daughter, we realize that the above equation is not enough as it does not take care of the few situations when the equation is not quadratic or has invalid coefficients. So eventually, we evolved to the following equation.

DECLARE @a FLOAT, @b FLOAT, @c FLOAT; SET @a = 2; SET @b = -5; SET @c = 3; -- Calculate the discriminant DECLARE @discriminant FLOAT; SET @discriminant = POWER(@b, 2) - (4 * @a * @c); SELECT x1_result = CASE WHEN @a = 0 OR @a IS NULL THEN 'Not a quadratic equation' WHEN @b IS NULL OR @c IS NULL THEN 'Invalid coefficients' WHEN @discriminant < 0 THEN 'No real solutions' ELSE 'Real solutions' END, x1 = CASE WHEN @a <> 0 AND @discriminant >= 0 THEN (-@b + SQRT(@discriminant)) / (2 * @a) ELSE NULL END, x2 = CASE WHEN @a <> 0 AND @discriminant >= 0 THEN (-@b - SQRT(@discriminant)) / (2 * @a) ELSE NULL END;

I noway claim that my T-SQL solution is 100% correct. I might have missed a few things here and there. Kindly correct me by providing more guidance.

You can always reach out to me on **LinkedIn**.

Reference:** Pinal Dave (https://blog.sqlauthority.com)**

## 2 Comments. Leave new

The given code appears to be a SQL script that calculates the discriminant and the solutions

(x1 and x2) for a quadratic equation with coefficients @a, @b, and @c. However, there are a

couple of issues with the code:

1. The condition in the first CASE statement is not necessary and can be simplified:

WHEN @a = 0 OR @a IS NULL THEN ‘Not a quadratic equation’

Since @a is defined as a FLOAT variable, it can never be NULL. Therefore, the condition @a

IS NULL is redundant, and we can safely remove it:

WHEN @a = 0 THEN ‘Not a quadratic equation’

2. The code does not handle the case when @a, @b, or @c is NULL. If any of these coefficients

is NULL, the calculation for the discriminant and the solutions will produce NULL results. To

handle this situation, we should add a separate condition to check for NULL coefficients:

WHEN @a IS NULL OR @b IS NULL OR @c IS NULL THEN ‘Invalid coefficients’

DECLARE @a FLOAT, @b FLOAT, @c FLOAT;

SET @a = 2;

SET @b = -5;

SET @c = 3;

— Calculate the discriminant

DECLARE @discriminant FLOAT;

SET @discriminant = POWER(@b, 2) – (4 * @a * @c);

SELECT

x1_result = CASE

WHEN @a = 0 THEN ‘Not a quadratic equation’

WHEN @a IS NULL OR @b IS NULL OR @c IS NULL THEN ‘Invalid coefficients’

WHEN @discriminant < 0 THEN 'No real solutions'

ELSE 'Real solutions'

END,

x1 = CASE

WHEN @a 0 AND @discriminant >= 0 THEN (-@b + SQRT(@discriminant)) / (2 * @a)

ELSE NULL

END,

x2 = CASE

WHEN @a 0 AND @discriminant >= 0 THEN (-@b – SQRT(@discriminant)) / (2 * @a)

ELSE NULL

END;

This revised code handles the case when coefficients @a, @b, or @c are NULL and provides appropriate messages accordingly. It calculates the discriminant and determines the nature of the solutions (real or non-real) for the given quadratic equation.

Thank you Yusep.