10

Why does this not work?

select *
from
(
    select membership_number
    from members
    where membership_number not like '%[^0-9]%'
) mem
where cast(membership_number as int) > 2

See SQL Fiddle Demo.

The subquery should filter out data that is non numeric, and the outer query is casting this to an integer so that I can look for anything > 2.

It seems like it is running the where clause of the outer query first. How do I get around this?

10
  • Have you run the inner query by itself to ensure it is returning what you expect? Apr 10, 2013 at 0:52
  • 2
    Thats cool. i never heard of SQL Fiddle before. Apr 10, 2013 at 0:52
  • 1
    If you look at the execution plan with a slightly modified WHERE it is rolling the sub-query where clause in with the main WHERE: Execution Plan I'm still thinking about how to prevent that behavior. Apr 10, 2013 at 0:57
  • 3
    Please check this - T-SQL functions do no imply a certain order of execution rusanu.com/2011/08/10/…
    – EricZ
    Apr 10, 2013 at 1:04
  • 1
    For your question, you can use WHERE ISNUMERIC(membership_number) = 1 AND cast(membership_number as int) > 2
    – EricZ
    Apr 10, 2013 at 1:05

6 Answers 6

2

Maybe that:

select *
from
(
    select
        membership_number
    from
        members
    where
        membership_number not like '%[^0-9]%'
) mem
where Try_Convert(int, membership_number) > 2
1
  • @EthanLi yes, but in question did not specify a version.
    – mkjasinski
    Apr 10, 2013 at 1:13
1

Very interesting, I tried to reproduce this on SQL Server and found next. I changed your query to simple just to make sure that query will not fail and I can see the execution plan:

select *
from
(
    select membership_number
    from members
    where membership_number not like '%[^0-9]%'
) mem
where membership_number > '2'

Execution plan is has Table Scan with predicate:

[master].[dbo].[members].[membership_number]>'2' 
    AND NOT [master].[dbo].[members].[membership_number] like '%[^0-9]%'

So this is because SQL Optimization engine works in this way (as somebody said - nobody can guarantee you the order of where clauses). One of the ways to fix it probably is to use ISNUMERIC before

select *
from
(
    select membership_number
    from members
    where membership_number not like '%[^0-9]%'
) mem
where ISNUMERIC(mem.membership_number) = 1 and cast(mem.membership_number as int) > 2
1

I had the issue before. What I did was:

1, you can have a view which does:

select membership_number
    from members
    where membership_number not like '%[^0-9]%'

2, or use temp table for it

3, or use case clause:

select *
from
(
    select membership_number
    from members
    where membership_number not like '%[^0-9]%'
) mem
where (CASE WHEN ISNUMERIC(membership_number) THEN cast(membership_number as int) ELSE 0 END) > 2

did not have a elegant solution, but hope this helps

0

The comments explain how the execution plan can (at times) choose to evaluate the cast prior to the like. A case statement can aid the order of evaluation but as Adams mentions even this method is not 100%.

select  *
from    members
where   case
            when membership_number like '%[^0-9]%' then 0
            when cast(membership_number as int) > 2 then 1
            else 0
        end = 1
3
  • 1
    CASE statements do not always short-circuit: Connect item Apr 10, 2013 at 1:32
  • @AdamWenger noted in post. +1
    – nathan_jr
    Apr 10, 2013 at 3:52
  • 1
    @AdamWenger: in the article, it mentions a specific case with aggregate functions, which are specified to be evaluated first always. In the OP's question, there's no aggregate function, so this shouldn't be a problem. And, as always, test to verify.
    – siride
    Apr 10, 2013 at 4:00
0

You can try this, in following query first condition is executed and if it fails then it won't executes 2nd condition

select
    membership_number
  from
    members
where 
isnumeric(membership_number) = 1 and 
cast(membership_number as int) > 2

And to answer why your query is not working check this explanation here

0

Old query, but still relevant.

Found this trick to force subquery to be run first. Try this:

select *
from
(
    select TOP (9223372036854775807) membership_number
    from members
    where membership_number not like '%[^0-9]%'
) mem
where cast(membership_number as int) > 2

Value 9223372036854775807 is the BIGINT max value, probably big enough for most cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.