IF in a CASE to assign a variable
I'm trying to create name variables on the fly. I do this with firstname and lastname fine but they are required in one table or the other. With middlename, they're not required in either. I have a case statement for each of these. In the second part of the middlename case statement, I need something that says, if it's it's null then don't add a '.', otherwise add it. Everytime I add an IF statement, I get an error.
Also tried keeping the statement as is then doing another, right after that says case when a.a_mname is null then '' as middlename, but it also didn't work.
The current query is below and thanks for any assistance.
SELECT p.publication_name,p.publication_id,o.dept,o.link as author_link,
CASE WHEN o.lname is not null THEN o.lname ELSE a.a_lname END as lastname,
CASE WHEN o.mname is not null THEN o.mname || '.' ELSE a.a_mname || '.' END as middlename,
CASE WHEN o.fname is not null THEN o.fname ELSE a.a_fname END as firstname,
a.publicationa_id
FROM publications p INNER JOIN publications_authors a ON p.publication_id = a.publication_id
LEFT JOIN people o ON a.id = o.id
ORDER BY publicationa_id asc