Problems with "IN" clause

I wrote an sql sentence while working on a problem that doesn't work the way I expect it to. I was wondering if anyone can explain to me why this doesn't work.

select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y")

does not return jobno=414335

However

select jobno from OEORD_T1 where jobno in (123456,456789,414335)

returns 414335 and

select jobno from UPSHH_T1 where OEUPDT_S="Y"

returns 414335

Furthermore

select oeord_t1.jobno from OEORD_T1 inner join  UPSHH_t1
on oeord_t1.jobno=upshh_t1.jobno where OEUPDT_S='Y'

returns 414335. I used this last statement instead of the first one to get the data that I needed. I don't understand why the original one didn't return the data I expected it to. I'm wondering if I don't understand how to use the IN clause correctly. Can anyone see what I'm doing wrong? Thanks in advance.


Kayser22Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
NULLs can "short-circuit"/"goof up" IN processing, so try this:

select jobno
from OEORD_T1
where jobno in (
    select jobno
    from UPSHH_T1
    where jobno IS NOT NULL
    AND OEUPDT_S="Y" )

although the join method is generally preferred.
0
Kayser22Author Commented:
That returned an identical recordset as the one I'm questioning. 414335 wasn't returned.
0
Scott PletcherSenior DBACommented:
Other than using single quotes instead of double, I don't see anything wrong, and it's very unlikely that you have a column named  y  on UPSHH_T1 table.


select jobno
from OEORD_T1
where jobno in (
    select jobno
    from UPSHH_T1
    where jobno IS NOT NULL
    AND OEUPDT_S='Y' )
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Kayser22Author Commented:
Actually the single quote version is what I started with. That returns the same recordset as the other two.

I'm using WIN uSQLi32 by transoft to query a transoft database. I hate to blame all my problems on buggy software, but maybe... Anyway here is the query plan I get when I run the previous query.

(Query: 1) OEORD_T1 INDEXED
Index No. 0
Lower : Set OEORD_T1.JOBNO to the Lowest of Subquery 1.1
Upper : OEORD_T1.JOBNO <= the Highest of Subquery 1.1
With Filter : OEORD_T1.JOBNO =any Results of Subquery 1.1

(Query: 1.1) UPSHH_T1 SEQUENTIAL
FILTER : ((not UPSHH_T1.JOBNO = null) and UPSHH_T1.OEUPDT_S = 'Y')
0
Scott PletcherSenior DBACommented:
Actually, buggy software is about the only explanation I can think of, either on the WIN uSQLi32 end or in SQL Server itself.  Otherwise, I would expect that query to work.
0
pra_kumar03Commented:
Does the datatype ofjobno match in both the tables?
0
Scott PletcherSenior DBACommented:
That's an EXCELLENT question, too.
0
pra_kumar03Commented:
first of all try this query and see if it returns something or not
select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y" and jobno = 414335)
0
Kayser22Author Commented:
select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y" and jobno = 414335)

does return 414335

The data types are the same, although in one table it is defined as a 3 byte field with a maximum of 999999 and the other table it is a 4 byte field with a maximum of 99999999.
0
Scott PletcherSenior DBACommented:
?

What, specifically, are the two datatypes/column definitions?

Are they, for example, CHAR(3)/VARCHAR(3) and CHAR(4)/VARCHAR(4)?
0
pra_kumar03Commented:
In case it is of the type as told by Scott then u may try to do ltrim(rtrim(jobno) in all the places and run the query
0
Kayser22Author Commented:
In transoft they are both defined as data type 2 which numeric.

Also, I didn't mean to imply anywhere that the problem sentence returns the null set. It does return 53 records; lowest= 406297 and highest =413256. I can't figure out why those get returned but 414335 does not.
0
namasi_navaretnamCommented:
Try this and see if the right rowcount is returned,

select count(*) from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S='Y')

Or Try

select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y")

Select @@ROWCOUNT

Is it possible that row is filtered by your front end tool.
HTH

Namasi
0
ShogunWadeCommented:
One thought.....    What is the collation on the OEUPDT_S field ?

If it is a case sensative collation you will need to UPPER()  your field before comparing.
0
ShogunWadeCommented:
ignore last,  ive just read the question properly :S

What about trying this

select jobno from OEORD_T1 a where exists (select 1 from UPSHH_T1 where OEUPDT_S="Y" and jobno=a.jobno)

?
0
Kayser22Author Commented:
Just catching up a bit. Due to normal operations, the original SQL sentence is now returning 11 records. 414335 is still in both tables.

select count(*) from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S='Y')

returns 11

select jobno from OEORD_T1 where jobno in (select jobno from UPSHH_T1 where OEUPDT_S="Y")

returns the same recordset as the original sentence.

select jobno from OEORD_T1 a where exists (select 1 from UPSHH_T1 where OEUPDT_S="Y" and jobno=a.jobno)

Timed out before returning results

0
ShogunWadeCommented:
Just trying an elimination process here.....

Are you sure there is no corruption in your database ?
0
Kayser22Author Commented:
Am I sure? No. Ususally any corruption manifests itself in the indexes, and I've rebuilt those. Ive checked the records in questions directly and they don't look corrupted. I don't know how to check for any other sorts of corruption.
0
ShogunWadeCommented:
try running DBCC CHECKDB('MyDb')

In the past I once had a table with a badly linked index page this meant that depending on whether the index was used depended upon whether the query returned the results :(

0
Computer101Commented:
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShogunWadeCommented:
Computer101:

3 days wasnt a very long time to post comments about this request given that a) the author seems to have gone silent since my last post.   and b) 3 days included xmas day,boxing day and saturday  :( :(

I am confused as to why the request was made.    Did our commets not resolve the problem?   If so why hasnt the author said so?   Did the author find the problem?

0
Kayser22Author Commented:
My apologies for not responding to your last entry. I started to and never finished. As I stated earlier, this isn't an SQL server. I believe the commands that you asked me to try are SQL server commands. I don't know of any equivalents in the transoft world. I originally asked this question to see if something was wrong with the SQL sentence I wrote, which I thought there probably was. Nobody found anything wrong with it. Unfortunately, I don't think that it is in the scope of my job, or my ability, to pursue this any further. Furthermore, the data changes on a daily basis, so my example is long gone. I don't want to waste any more of anybody's time, so I asked for the issue to be closed. I appreciate everybody's efforts at EE and don't want to treat anybody unfairly. If you can give me any feedback on how I could have handled this better, I'm all ears.

0
ShogunWadeCommented:
Ok.  Sounds fair enough.   I just was a bit mistified following a spell of silence and then a closure request.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.