Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

where not in ( int value and null)

Posted on 2011-02-16
8
Medium Priority
?
290 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

618 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