"Invalid use of Null" error in query

The query below gives me an "Invalid use of Null" error.


DELETE *
FROM [Session]
where cptcode is not null and (cint(cptcode) > 100 and cint(cptcode) < 200);
rrhandle8Asked:
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.

Dan CraciunIT ConsultantCommented:
Try

DELETE *
FROM [Session]
where not isnull(cptcode) and (cint(cptcode) > 100 and cint(cptcode) < 200);

HTH,
Dan
0
hnasrCommented:
Try:

DELETE *
FROM [Session]
Where CInt(nz(cptcode,0) > 100 and CInt(nz(cptcode,0) < 200;
0
IrogSintaCommented:
Slight correction to hnasr's answer:
DELETE *
FROM [Session]
Where CInt(nz(cptcode,0)) > 100 and CInt(nz(cptcode,0)) < 200;

You can also use the Between keyword:
DELETE *
FROM [Session]
Where CInt(nz(cptcode,0)) Between 101 And 199;
0
Gustav BrockCIOCommented:
Your problem is that the where clause:

  where cptcode is not null and (cint(cptcode) > 100 and cint(cptcode) < 200);

evaluates all expressions with all values, thus Null is passed to CInt which doesn't accept Null.

The solution is not a workaround but to replace CInt with Int which is native to SQL and does accept Null:

  Where cptcode Is Not Null And Int(cptcode) > 100 And Int(cptcode) < 200;

Further, you could remove CInt/Int and adjust for the same result:

  Where cptcode Is Not Null And cptcode >= 101 And cptcode < 200;

/gustav
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
rrhandle8Author Commented:
DELETE *
FROM [Session]
where not isnull(cptcode) and (cint(cptcode) > 100 and cint(cptcode) < 200);

Still returns error.

----------------------------------------------------------

DELETE *
FROM [Session]
Where CInt(nz(cptcode,0)) > 100 and CInt(nz(cptcode,0)) < 200;

This work

----------------------------------------------------------

DELETE *
FROM [Session]
Where cptcode Is Not Null And Int(cptcode) > 100 And Int(cptcode) < 200;

This also worked, and cactus_data added note about CInt and Int.
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 Development

From novice to tech pro — start learning today.