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
 Blank where clause still pulls data, why?

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2012-05-21 : 20:44:35
When I run
Select * from dbo.vwSectionMenuItems where SectionID = '' and MenuType = 'Info'

It returns the results that have Section = 0 why is that? Since the where clause has SectionID = '' shouldnt that return only return results with nothing set in SectionID? (Note: SectionID is a tinyint)

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-21 : 20:50:00
SectionID is tinyint

In your where clause
SectionID = ''

the empty string '' will be converted to integer and it will be 0


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-21 : 20:50:44
or is this what you are after ?
where SectionID IS NULL and MenuType = 'Info'



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2012-05-21 : 20:55:32
quote:
Originally posted by khtan

SectionID is tinyint

In your where clause
SectionID = ''

the empty string '' will be converted to integer and it will be 0


KH
[spoiler]Time is always against us[/spoiler]





That makes sense, thanks

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-22 : 05:29:38
Dont use empty string for non character datatypes
http://beyondrelational.com/modules/2/blogs/70/posts/10841/empty-string-and-default-values.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2012-05-23 : 07:00:39
quote:
Originally posted by madhivanan

Dont use empty string for non character datatypes
http://beyondrelational.com/modules/2/blogs/70/posts/10841/empty-string-and-default-values.aspx

Madhivanan

Failing to plan is Planning to fail


I am not all data in the DB either has a value or is explicitly set to NULL.

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page
   

- Advertisement -