I'm trying to write a simple IF/ELSE statement in Teradata. As far as I understand you have to use a CASE. What I want to do is write a statement that will check if a column IS NOT NULL and display something. ELSE if it IS NULL, display something else.
Every example I found simply replaces a single value with a hard coded string or int. I'm looking for something more along the lines of this which uses the THEN statement to make another SELECT:
SELECT CARS.VIN_NUM, CARS.DRIVER_NAMES
CASE
WHEN CARS.FUEL IS NOT NULL THEN SELECT CARS.DESTINATIONS
WHEN CARS.FUEL IS NULL THEN SELECT CARS.GAS_STATIONS
END
FROM AUTOMOBILES CARS
WHERE CARS.VIN_NUM IN
('345353',
'354632',
'535231')
ORDER BY CARS.VIN_NUM
The end result should be a table displaying the VIN_NUM, DRIVER_NAMES, DESTINATIONS OR GAS_STATIONS based on the CASE. Is something like this possible or am I going about it the wrong way?
If DESTINATIONS
and GAS_STATIONS
are columns in the table AUTOMOBILES
(aliased CARS
) then the following should work:
SELECT CARS.VIN_NUM,
CARS.DRIVER_NAMES,
CASE
WHEN CARS.FUEL IS NOT NULL THEN CARS.DESTINATIONS
ELSE CARS.GAS_STATIONS
END
FROM AUTOMOBILES CARS
WHERE CARS.VIN_NUM IN
('345353',
'354632',
'535231')
ORDER BY CARS.VIN_NUM;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.