Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I make this a 0 if it returns null? Thanks.
C++
sprintf(queryString, "SELECT (IPType) FROM tblURLIP WHERE IP = '%s'",ipSrc);
Posted

Hi,

I would personally use a ternary statement:

C++
sprintf(queryString, "SELECT (IPType) FROM tblURLIP WHERE IP = '%s'",NULL == ipSrc?"0":ipSrc);


Best Wishes,
-David Delaune
 
Share this answer
 
Comments
Member 7766180 14-Sep-11 23:16pm    
Thank you. Does this give 0 whether IPType is NULL or ipSrc is NULL or both?
[no name] 14-Sep-11 23:33pm    
If ipSrc is NULL then "0" is the string that will be written into the format tag: '%s'

Best Wishes,
-David Delaune
Member 7766180 14-Sep-11 23:42pm    
Thank You. I'm sorry that I wasn't clear. I will always have an IP. I just what to get the IPType from the database. However sometimes the IP will not be in the database and thats when I want it to return 0.
[no name] 14-Sep-11 23:55pm    
The value IPType will be returned by the SQL database. You will need to show more code if you want a C++ answer. If your looking for a SQL answer it could be: "SELECT ISNULL(MAX(IPType), 0) FROM tblURLIP WHERE IP = '%s'"

Best Wishes,
-David Delaune
Member 7766180 15-Sep-11 0:04am    
Thats it! SQL Answer. Thank you!
I am used to the below solution

SQL
Select COALESCE(IPType,0) From WHERE IP = '%s'
 
Share this answer
 
v2
Comments
Member 7766180 17-Sep-11 10:47am    
Thank You!

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