Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
SELECT R.[FORM_ID], R.APPROVAL_STATUS,A.FORM_ID,A.APPROVAL_DECISION, S.FORM_ID,S.APPROVAL_DECISION,
ISNULL(S.APPROVAL_DECISION,A.APPROVAL_DECISION)  AS AD3
FROM [PREV_REQUEST_TO_RECRUIT] AS R
LEFT JOIN [HR_AUTHORISATION] A  ON  R.FORM_ID=A.FORM_ID
left join [SEN_MGR_AUTHORISATION] S on R.FORM_ID = S.FORM_ID




Could you please help me in getting the value of R.APPROVAL_STATUS into the same column AD3 when both S.APPROVAL_DECISION,A.APPROVAL_DECISION is null.


Thanks,
Sathish
Posted
Updated 17-Oct-13 4:57am
v2

1 solution

COALESCE should help you in this case. It returns the first non-null value.

SQL
SELECT COALESCE(S.APPROVAL_DECISION,A.APPROVAL_DECISION, R.APPROVAL_STATUS) AS SomeColumn


Or, if for some reason you don't want the first 2 values and your condition is to get the third only when the first 2 are null you can use a CASE statement.

SQL
SELECT CASE WHEN S.APPROVAL_DECISION IS NULL AND A.APPROVAL_DECISION IS NULL THEN R.APPROVAL_STATUS ELSE 'WhateverYouWant' END AS SomeColumn
 
Share this answer
 
Comments
sathish kumar 17-Oct-13 11:14am    
SELECT R.FORM_ID,R.APPROVAL_STATUS, A.FORM_ID,A.APPROVAL_DECISION, S.FORM_ID,S.APPROVAL_DECISION,
COALESCE(S.APPROVAL_DECISION,A.APPROVAL_DECISION, R.APPROVAL_STATUS) AS SomeColumn
FROM [PREV_REQUEST_TO_RECRUIT] AS R
LEFT JOIN [HR_AUTHORISATION] A ON R.FORM_ID=A.FORM_ID
left join [SEN_MGR_AUTHORISATION] S on R.FORM_ID = S.FORM_ID

Please check coalesce query it's not working for me only returns the S.APPROVAL_DECISION value
ZurdoDev 17-Oct-13 11:19am    
That's because S.APPROVAL_DECISION is not null. Please read my notes again. COALESCE will return the first non-null value it finds. It sounds like you may want to use my second example instead.
sathish kumar 17-Oct-13 11:23am    
thanks for your reply, I deleted all the values and made it null it showed me null.
then i added value in r.approval_status its null
then i added value in a.approval_decision its null
and finally s.approval_decision now it returns the value.
so it just displaying me s.approval_decision value ... could you please check my coalesce condition which I posted is right?
ZurdoDev 17-Oct-13 11:41am    
I am not sure what is "right" because it depends on what you need. Again, COALESCE(1,2,3,4,5,6, etc,) will just find the first non-null value from left to right.
sathish kumar 17-Oct-13 11:43am    
Thanks, you are right. it was my mistake I thought its right to left :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900