SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to check Zero against blank
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vijays3
Constraint Violating Yak Guru

India
343 Posts

Posted - 10/31/2013 :  10:23:20  Show Profile  Reply with Quote
Hi All

This below code is saying 0 is equal to space .How can I avoid
this situation.It is saying 'a is blank' but i have assigned var
@a as a 0.

DECLARE @a int =0
IF @a =''
BEGIN
SELECT 'a is blank'
END
ELSE
SELECT 'a is not blank'



Vijay is here to learn something from you guys.

khtan
In (Som, Ni, Yak)

Singapore
17681 Posts

Posted - 10/31/2013 :  10:28:23  Show Profile  Reply with Quote
for the condition @a = ''
it is doing an implicit conversion of empty string '' to integer which resulted in 0

see http://msdn.microsoft.com/en-us/library/ms191530.aspx

since @a is 0, the IF condition is true anad the 'a is blank' is printed


KH
Time is always against us

Go to Top of Page

vijays3
Constraint Violating Yak Guru

India
343 Posts

Posted - 10/31/2013 :  10:35:59  Show Profile  Reply with Quote
Thanks ..But how can I avoid this any clue..

Vijay is here to learn something from you guys.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17681 Posts

Posted - 10/31/2013 :  10:43:25  Show Profile  Reply with Quote
don't perform such comparison like
@a = ''

you should either declare @a as a string variable or compare @a with 0

like IF @a = 0


KH
Time is always against us

Go to Top of Page

vijays3
Constraint Violating Yak Guru

India
343 Posts

Posted - 10/31/2013 :  10:48:21  Show Profile  Reply with Quote
Thanks for your suggestion.

Vijay is here to learn something from you guys.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000