Mariadb Case Statement

Mariadb Case Statement

I have the following SQL-Code in a Mariadb-Database: (1)

select Labornummer, Matrix, FaktorGW, FaktorAW
from gc_Faktoren

I need the following result:

If Matrix='AW' => I need the field "FaktorAW"

else => I need the field "FaktorGW"

is it possible to formulate Statement (1) with a "case statement"?

2 Answers

Of course, this is possible. Basically you can do this:

SELECT labornummer, matrix, faktoraw, faktorgw,
CASE WHEN matrix = 'AW' THEN faktoraw
ELSE faktorgw END AS factor
FROM gc_faktoren;

You have to take care if this is really exactly what you want, e.g. this will not check lower/upper case. See the working example: db<>fiddle

Try

select Labornummer, Matrix, FaktorGW, FaktorAW, 
CASE
 WHEN Matrix = 'AW' THEN FaktorAW ELSE FaktorGW END as New_Field
from gc_Faktor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.