I am looking to select from three columns using three parameters, one for each column. If all the parameters are present for the columns then it will return a fourth column. If, however, any one of the three parameters is not found in the table, then it should return a message informing the user which of the parameters is incorrect.
I have tried the following:
SELECT RESULTS, "[status]" =
case
when CostCenterNo <> '800' then 'CentreNotFound'
when EmpNo <> '2' then 'EmpNotFound'
when Surname <> 'sonny' then 'SurnameNotFound'
else null
end
from CostCentres
where
CostCenterNo =
case CostCenterno when
'800' then '800'
else ''
end
and
EmpNo =
case EmpNo when
'2' then '2'
else ''
end
and
Surname =
case Surname when
'sonny' then 'sonny'
else ''
end
this retrieves the correct information when all parameters are correct but then I need for it to say CentreNotFound, EmpNotFound, SurnameNotFound for when the respective parameter is not found in the table. I have tried looking up the links below but still no luck. IF statement in SQL Server where clause
Using EXISTS as a column in TSQL
SQL: IF clause within WHERE clause
I have also tried the following code:
select results =
case when CostCenterNo = '800' then
case when EmpNo = '2' then
case when Surname = 'sonny' then
(select results from bcse
where CostCenterNo = 'BW800'
and EmpNo like '2'
and Surname = 'sonny')
else 'surname not found' end
else 'Emp not found' end
else 'center no not found' end
from CostCentres
where (CostCenterno = 'bw800' or CostCenterNo = '%')
and (EmpNo = '2' or EmpNo = '%' )
and (surname = 'sonny' or surname = '%')
The above works for the correct parameters but then again I need it to return the CentreNotFound, EmpNotFound, SurnameNotFound for when the respective parameter is not found in the table.