• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

SQL question about returning a row even if one of the selects are null

JMaster

Golden Member
I'm writing a SQL query (Postgresql) where I'm grabbing 6 different things:
i.e. select a, b, c, d, e, etc...
If e is empty, I still want to pull that record with e = null. Am I supposed to use coalesce(e, 'null')?

By the way, e comes from I.g = J.g AND J.f = K.f AND K.e = L.e, where I have I.g to start with.
Sometimes J has a value for the "e column" and sometimes it doesn't. If it has it, I want to get e and if it doesn't, I want to get "null".
 
In mysql, you can do

"SELECT a, b, c, d, IFNULL(e, 'null')"

try it in postgres and see how it goes, though make sure you're using the right null checking function.
 
So you want to get a string back with the word "null" instead of an sql null value? Both your solution and HJB417s sound reasonable.

I don't know why you'd ever want to do that though. Sql null was created like it is for a reason. A better solution would be to detect the null at the app level and substitute the string yourself. That way you don't get screwed by the odd chance that e is actually the string "null" in the database.
 
Back
Top