Click here to Skip to main content
15,891,847 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI guys,
I wrote a stored proc to get the customer details based on User email or Phone number. i used coalesce to wrote this sp. i got the results when User email and phone number columns data exists. but, i am unable to get the results when column data is null.
Please help me to fix this.
Thanks

What I have tried:

SQL
select UserId,UserEmail,Code,PhoneNo,Name,UserImage,U.CreatedDateTime,A.Country,U.UserImageUrl,
 from Users U ,Address A where
[UserEmail]=coalesce('email@gmail.com',UserEmail)  and
[PhoneNo]=coalesce(null,PhoneNo) and 
A.Id=U.AddressId 
and A.CreatedUserId=U.UserId
Posted
Updated 12-Jul-17 19:33pm
v2
Comments
Richard Deeming 11-Jul-17 11:42am    
NB: You should avoid using the (deprecated) old-style joins, and instead use the ANSI-92 join syntax:
from Users U INNER JOIN Address A ON A.Id = U.AddressId And A.CreatedUserId = U.UserId
.net333 13-Jul-17 1:12am    
Thank you Richard. i will follow the ANSI-92 Join

I solved this by using
COALESCE([Useremail],'')=coalesce(@Email,[UserEmail],'') and
COALESCE([PhoneNo],'')=coalesce(@PhoneNumber,[PhoneNo],'')


It handles the null values with coalesce function
 
Share this answer
 
Comments
Maciej Los 13-Jul-17 2:02am    
+5 as an incentive to work/study.
At this moment coalesce returns always email@gmail.com, as it is non-null.

So, replace:
[UserEmail]=coalesce('email@gmail.com',UserEmail)

with:
[UserEmail]=coalesce(UserEmail,'email@gmail.com')


BTW:
I don't understand the usage of: [PhoneNo]=coalesce(null,PhoneNo). This means: replace null with first non-null value of PhoneNo.
Check the documentation as an0ther1[^] mentioned in his anwer.
 
Share this answer
 
v2
Comments
.net333 13-Jul-17 1:15am    
Thanks Maciej Los. I need to get the User Details with Email or Phone Number. That's why i used coalesce function.
.net333 13-Jul-17 1:32am    
I solved this by using
COALESCE([Useremail],'')=coalesce(@Email,[UserEmail],'') and
COALESCE([PhoneNo],'')=coalesce(@PhoneNumber,[PhoneNo],'').

It handles the null values with coalesce function
Maciej Los 13-Jul-17 2:00am    
Great!
Cheers,
Maciej
Coalesce returns the first non-null value from your expression - refer;
MSDN - COALESCE (Transact-SQL)[^]
Perhaps you should be using ISNULL - refer;
ISNULL (Transact-SQL) | Microsoft Docs[^]

Kind Regards
 
Share this answer
 
Comments
Maciej Los 11-Jul-17 3:15am    
A4! Refer to documentation is always good option, but you have to explain what OP is doing wrong.
an0ther1 11-Jul-17 22:00pm    
Thank Maciej
.net333 13-Jul-17 1:16am    
Thanks an0ther1. i will refer the documents
an0ther1 13-Jul-17 1:28am    
No problems buddy.
I read your reply to Maciej - if you need to get either the phone number or email you can use coalesce as follows
[ContactPhoneEmail] = COALESCE(UserEMail, PhoneNumber, 'email@gmail.com')

Cheers
.net333 13-Jul-17 1:30am    
I solved this by using
COALESCE([Useremail],'')=coalesce(@Email,[UserEmail],'') and
COALESCE([PhoneNo],'')=coalesce(@PhoneNumber,[PhoneNo],'').

It handles the null values with coalesce function

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