Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to check Zero against blank

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-10-31 : 10:23:20
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)

17689 Posts

Posted - 2013-10-31 : 10:28:23
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
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-10-31 : 10:35:59
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)

17689 Posts

Posted - 2013-10-31 : 10:43:25
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
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-10-31 : 10:48:21
Thanks for your suggestion.

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

- Advertisement -