?
Solved

where not in ( int value and null)

Posted on 2011-02-16
8
Medium Priority
?
285 Views
Last Modified: 2012-05-11
statusfk is supposed to be null or have an int value that is 8 numbers. but i saw in the table there is one with 7 digits.

but the following query
select * from tblsinger where len(statusfk) not in (8, null)
did not bring back the records for statusfk with 7 length.

what is wrong with the query?

thanks
0
Comment
Question by:anushahanna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 668 total points
ID: 34910332
NOT IN + NULL = bad query

NULL is not comparable to anything, so if it is present in the NOT IN clause at all, it is as good as filtering everything out.
0
 
LVL 9

Assisted Solution

by:AriMc
AriMc earned 332 total points
ID: 34910350
LEN function works on character fields only. For integers you could try:

select * from tblsinger where statusfk between (1000000 and 9999999)

0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 668 total points
ID: 34910352
1 you can only test a column against null with the IS NULL/IS NOT NULL SYNTAX
2  a null value "doesn't" have a length
3 len is for character columns only

so try

select * from tblsinger
 where statusfk < 10000000
 
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 6

Author Comment

by:anushahanna
ID: 34910429
>>NOT IN + NULL = bad query
>>you can only test a column against null with the IS NULL/IS NOT NULL SYNTAX

THANK YOU....

now,
select * from tblsinger where statusfk is not null and len(statusfk)<8
worked
(len does work for int - can you please test that)
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 332 total points
ID: 34910918
Yes, you can check the no. of digits with LEN.
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 668 total points
ID: 34911110
you can "check" with len on the size of the converted string... from the underlying numeric data type

if the statusfk is indexed then the len(statusfk) check makes it non sargable  (less likely to use an index efficiently)

where as the numeric range condition does not suffer from that problem...

in the general case you may want to consider the effect of negative numbers as well
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 668 total points
ID: 34912850
@AriMC "LEN function works on character fields only. For integers you could try:"
@Lowfatspread

Try select LEN(1234), LEN(1234222)

> have an int value that is 8 numbers

So Len( ) does work to flush it out.

@Lowfatspread

While you are right about performance, the question is worded as a one-off so by the time you posted that tidbit, the problem has been solved...
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34917557
thanks for your helpful explanation.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question