I’m trying to write if..else condition inside sql what I want to get the company name from “Company” table if their are companyID in companyID is NULL print “N/A” con someone help me
SELECT [contactID]
,[customerID]
,(SELECT Label.labelContactType FROM Label WHERE Label.labelContactTypeID = Customer_Contacts.labelContactTypeID)AS Type
,[contactDetails]
,[status]
,[notes]
,CASE WHEN [Customer_Contacts].companyID = NULL THEN 'N/A'
WHEN [Customer_Contacts].companyID <> NULL THEN (SELECT [companyName]
FROM [TaskManagementSystem_DB].[dbo].[Company] WHERE [Company].companyID = [Customer_Contacts].companyID)
END AS Company
FROM [TaskManagementSystem_DB].[dbo].[Customer_Contacts]
WHERE customerID = 24
Why not just use a
JOINfor this query:If you need help with join syntax, here is a great visual explanation of joins