How do I replace NULL values with 'N/A' in SQL? [duplicate] - mysql

This question already has answers here:
MySql Query Replace NULL with Empty String in Select
(10 answers)
Closed 4 months ago.
I am looking to replace the NULL values that occur as a result of a SQL JOIN statement, with 'N/A'.
I have tried to set the default value of both related columns from both tables to N/A, however, every time I execute the SQL JOIN statement, I still receive NULL values.
The two tables I have are the clients and Medical_Aid tables, which I have connected using a foreign key called Reg_No. Below is my sql join query
SELECT
clients.Id_Number,
clients.Medical_No,
medical_aid.Name AS Medical_Aid,
clients.First_Name,
clients.Last_Name,
clients.Age,
clients.Gender,
clients.Email,
clients.Telephone
FROM
clients
LEFT OUTER JOIN medical_aid ON clients.Reg_No = medical_aid.Reg_No;
I have tried to set the default value of the Medical_No and Medical_Name as 'N/A' but every time I execute a JOIN statement, NULL values are returned on the Medical_Name column only
Therefore, I am expecting the JOIN Statement to return 'N/A' for both the Medical_No and medical_AidName

SELECT
clients.Id_Number,
ISNULL(clients.Medical_No,'N/A'),
ISNULL(medical_aid.Name, 'N/A') AS Medical_Aid,
clients.First_Name,
clients.Last_Name,
clients.Age,
clients.Gender,
clients.Email,
clients.Telephone
FROM
clients
LEFT OUTER JOIN medical_aid ON clients.Reg_No = medical_aid.Reg_No;

For the values from the medial_aid table you can use the IsNull() function to replace a NULL with a different value:
IsNull(medical_aid.Name, 'N/A') AS Medical_Aid
If you want to also replace another field from the clients table when no record is found in the medical_aid table, you may need to use a CASE statement:
CASE WHEN medical_aid.Reg_No is null THEN 'N/A' else clients.Medical_No END AS Medical_No
This statement says that when medical_aid.Reg_No is NULL (since there was no record from the medical_aid found to join to the clients table) then output 'N/A', otherwise output clients.Medical_No.

Related

Need to join SQL table with multiple match, if value doesn't matches it should return a blank/NA value

I want to join below tables in such a way that that it should return matched value if not matching it should return a value as "NA".
below are the table details
available tables:
desired output:
Use Left Join between the tables, to consider those rows also which do not exist in the Process table.
You can use Ifnull() function, to set the value NA if no matching row (thus null value) in the Process table.
Try the following (change table and column name(s) accordingly):
SELECT r.Ticket_id,
r.Status,
r.Department,
r.Owner,
r.Process_id,
IFNULL(p.Proces_Name, 'NA')
FROM Resolution AS r
LEFT JOIN Process AS p ON p.Process_id = r.Process_id
If a value isn't found in a MySQL LEFT JOIN, the field is "filled" with NULL
The best practice, as I understand your question is IFNULL
SELECT
r.*,
IFNULL(process_name,'NA')
FROM
resolution r
LEFT JOIN processes p ON p.process_id = r.process_id
select a.ticket_id,a.status,a.department,a.ownername,
case
when b.process_name is null then 'NA'
else b.process_name
end as proces_name
from resolution_tab a
left join process_tab b
on a.process_id=b.process_id
order by a.ticket_id;

mysql query return null row [duplicate]

This question already has answers here:
SELECT SUM returns a row when there are no records
(8 answers)
Closed 7 years ago.
i want to execute this query but it return null row when the table empty. it's working when SUM(products.sale_price)/COUNT(orders.id) AS avg_price_rang
is not in the query
SELECT
products.brand_id,
(SELECT
category_brands.name
FROM
category_brands
WHERE
category_brands.id=products.brand_id
) AS brand,
products.material_id,
(SELECT
category_materials.material
FROM
category_materials
WHERE
category_materials.id=products.material_id
) AS material,
orders.color_code,
SUM(products.sale_price)/COUNT(orders.id) AS avg_price_rang
FROM
orders
INNER JOIN
products
ON
orders.prodcut_id = products.id
I think it's missing GROUP BY products.brand_id.
SELECTing SUM when there are no rows causess a row of all NULLs to be returned.
MySQL 5.5 Reference Manual
12.17.1 GROUP BY (Aggregate) Functions
If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows. For more information, see Section 12.17.3, “MySQL Handling of GROUP BY”.
Use:
query
HAVING avg_price_rang IS NOT NULL
SELECT * FROM (
query
) dummy
WHERE avg_price_rang IS NOT NULL

MYSQL - COUNT() NULL Values

This has been racking my head. I've scoured the internet (including this place) and can't find a solution. So as a last resort I was hoping the good people of this forum might be able to help me out.
I have two tables:
TableA
Order_detailsID
OrderID
TitleID
Return_date
TableB
TitleID
Title_name
Quantity_in_stock
And would like to run a query that shows the remaining 'Quantity_in_stock'.
If the 'Return_date' is set to NULL then it means the item is currently out -- so I have been trying to use the count() function for the NULL values and subtract it from the 'Quantity_in_stock'.
This is the script I have so far:
DELIMITER //
CREATE PROCEDURE InStock()
BEGIN
Select TableB.TitleID,
TableB.Title_name,
TableB.Quantity_in_stock AS 'Total_Stock',
COUNT(TableA.return_date IS NULL) AS 'Rented_Out',
TableB.Quantity_in_stock - COUNT(TableA.return_date IS NULL) AS 'Remaining Stock'
From TableB
LEFT JOIN TableA
ON TableA.TitleID = TableB.TitleID
GROUP BY TableB.TitleID;
END//
This works if there is one of more of the TitleIDs at NULL, however if there are no values at NULL, then the Count() is still returning a value of 1 when it should be 0.
What am I doing wrong?
Instead of:
COUNT(TableA.return_date IS NULL)
use this:
SUM(CASE
WHEN TableA.TitleID IS NULL THEN 0
WHEN TableA.return_date IS NOT NULL THEN 0
ELSE 1
END)
The problem with the TableA.return_date IS NULL predicate is that it's true in two completely different situations:
When there is no matching record in TableA
When there is a matching record but TableA.return_date value of this exact record is NULL.
Using the CASE expression you can differentiate between these two cases.
I will like to mention a simple concept here, just keep counting the rows when that particular column is null.
select count(*) from table_name where column_name is null

Joining with NULL values

How deals with MySQL JOIN deal with NULL values. Being that most address do not have a second line address, there are many NULL values for address2. When I run the query to match up address based on address1, address2 and zip, the query will not return positive matched of when only address1, and zip match. I have ensured that both tables have a value of NULL for address2, when they are NULL. So basically to run the query I run two queries.
UPDATE target t
JOIN pl2.site2 s ON t.address1=s.address1 AND t.zip5=s.zip5
SET t.idsite=s.idsite
WHERE t.address2 IS NULL AND s.address2 IS NULL;
UPDATE target t
JOIN pl2.site2 s ON t.address1=s.address1 AND t.address2=s.address2 AND t.zip5=s.zip5
SET t.idsite=s.idsite;
Not really a hard work around. Just trying to get more into the science of how the SQL works. Maybe there was another solution, I don't want to set the columns=''(blank) where they are NULL either.
You have a couple of options to join the null fields. One is to use COALESCE which converts the null values to blanks for the comparison:
UPDATE target t
JOIN pl2.site2 s ON
t.address1=s.address1 AND
COALESCE(t.address2,'')=COALESCE(s.address2,'') AND
t.zip5=s.zip5
SET t.idsite=s.idsite;
Another option would be to use OR in the JOIN:
UPDATE target t
JOIN pl2.site2 s ON
t.address1=s.address1 AND
t.zip5=s.zip5 AND
((t.address2=s.address2) OR (t.address2 IS NULL AND s.addresss2 IS NULL))
SET t.idsite=s.idsite;

mysql getting null values while joining 3 tables

when I try to get results from 3 tables, I only get info from 2, the third one returns only NULL values.
This is my query, I am running it in phpmyAdmin to test it out before putting it on server:
SELECT uk2.*,u.*,COALESCE(SUM(n.cost),'not found') as cost
FROM std13_k2_users AS uk2
JOIN std13_users AS u
ON uk2.userID=u.id
JOIN std13_k2_naklady AS n
ON uk2.userID=n.userID WHERE uk2.userID=986
I get values from all tables when the SUM is not NULL (there are no things to sum in that table). When it is NULL I only get values from std13_users table. The values from std13_k2_users are NULL.
My question is: how should I repair my query? Thanks.
Try this please, assuming that your joins correctly done for your table schema.
SELECT uk2.*,u.*, case when SUM(COALESCE(n.cost,0)) = 0 then 'not found'
else SUM(COALESCE(n.cost,0)) end as cost
FROM std13_k2_users AS uk2
JOIN std13_users AS u
ON uk2.userID=u.id
JOIN std13_k2_naklady AS n
ON uk2.userID=n.userID WHERE uk2.userID=986