Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:48:11+00:00 2026-06-13T07:48:11+00:00

I’ve got one table with alphanumeric codes. In this particular instance, I am not

  • 0

I’ve got one table with alphanumeric codes. In this particular instance, I am not concerned with the codes that are alphanumeric in nature, only those that contains integers. I wrote a small query to find a the subset of codes that I needed:

 select CAST(icd as float) as icd

from
(
select i.icd
    from icd as i
    where icd like '952%' or icd like '806%' or icd like '3440%' or icd like '3441'
)t

This returns me a list of the codes I needed. However, when I try to use a where clause like:

where icd between 80600 and 80610 the query fails, telling me that it can’t convert the varchar datatype to int. However, if I do something like

select CAST(icd as float) as icd,icd+10

from
(
select i.icd
    from icd as i
    where icd like '952%' or icd like '806%' or icd like '3440%' or icd like '3441'
)t

I can add ten to each code and it runs, meaning that they are in fact integers. I want to use the where clause like this because codes 80600-80610 should be labeled X and 80611-80620 they should be labeled Y. I can do this manually for each code, but I’d like to be more extensible than that.

How can I make sure that SQL Server only looks at the derived table when using this where clause, instead of failing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:48:12+00:00Added an answer on June 13, 2026 at 7:48 am

    Filters run before results are cast

    What you’re doing is casting results not column values when query runs (and does all the filtering). Filters (where clause) are evaluated before result values are being cast.

    In order to still work with the same result set as you show in your example the easiest way is to use a CTE instead of a sub-query and then do additional filtering on that:

    with codes as (
        select cast(icd as int) as icd
        from icd
        where icd like '952%' or
              icd like '806%' or
              icd like '3440%' or
              icd like '3441'
    )
    select icd, 'X' as label
    from codes
    where icd between 80600 and 80610
    union all
    select icd, 'Y'
    from codes
    where icd between 80611 and 80620
    

    Whether CTEs are more/less readable than sub-queries is a matter of endless discussion/arguments. But in this case where we reuse the same table expression to get X and Y labelled results it uses less code compared to what would be needed when doing the same with sub-queries. In this case it is by far more readable.

    Data to be cast must be numeric of course

    When you’re casting data you have to make sure it actually can be cast to result type. If your value is 064 V this is clearly not a number hence can’t be cast. a call to isnumeric can help here:

    with codes as (
        select cast(icd as int) as icd
        from icd
        where isnumeric(icd) = 1 and
              (
                  icd like '952%' or
                  icd like '806%' or
                  icd like '3440%' or
                  icd like '3441'
              )
    )
    select icd, 'X' as label
    from codes
    where icd between 80600 and 80610
    union all
    select icd, 'Y'
    from codes
    where icd between 80611 and 80620
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I have a small JavaScript validation script that validates inputs based on Regex. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.