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 604693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:03:56+00:00 2026-05-13T17:03:56+00:00

I have a column TypeCode varchar(20) that has values like ‘1’, ‘2’, ‘3’, ‘FOO’,

  • 0

I have a column TypeCode varchar(20) that has values like ‘1’, ‘2’, ‘3’, ‘FOO’, ‘BAR’. I need to find the maximum integer which is less than the value of parameter. Something like this:

select max(TypeCode) TypeCode
    from table1 a
    left join table2 b on b.table1id = a.id
        and b.TypeCode not in ('FOO', 'BAR')
    where b.TypeCode < @MaxType

Which works most of the time, but in some queries SQL Server decides to convert it to something like this (according to the query plan).

select max(TypeCode) TypeCode
    from table1 a
    left join table2 b on b.table1id = a.id
        and b.TypeCode < @MaxType
        and b.TypeCode not in ('FOO', 'BAR')

That query obviously produces the following error:

Conversion failed when converting the varchar value 'FOO' to data type int.

I tried creating a view of table2 without the ‘FOO’ and ‘BAR’ values and joining the view instead but the query plan is still the same.

Do you know of a way to prevent the optimizer from changing the query?

PS: I know the design of the table is not the best but this is a legacy database and I can’t change it.

  • 1 1 Answer
  • 2 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-05-13T17:03:57+00:00Added an answer on May 13, 2026 at 5:03 pm

    The real problem here is that you are combining character and integer semantics in the same query.

    There are three possible solutions that I can think of:

    1. Change the @MaxType parameter to be a varchar(9). If you are only comparing against single-digit numbers, alphabetical order is OK. Otherwise, this won’t work.

    2. Use a CASE or ISNULL(NULLIF(...)) construct in the WHERE statement. This will work, but it is not sargable and will cause the optimizer to ignore any indexes you have on the type code. Not great.

    3. Created a persisted, computed, nullable integer column (i.e. TypeCodeID) and index it separately. Put the CASE as the column’s expression. This will take some extra data/index space, but if you want good performance, that is the best way to go. Then instead of writing NOT IN ('Foo', 'Bar') you can just write the first condition (TypeCode < @MaxType) because the rows with Foo and Bar in the TypeCode column will have NULL in the new TypeCodeID column.

    I suppose there is also a fourth answer which is to change your design, and this would be the best idea, if it’s possible. If a column can contain character data then you really shouldn’t be trying to perform numerical comparisons. I have a strong suspicion that the character data exists in this column because it’s coming from users and isn’t validated/sanitized properly, so it’s got garbage values like N/A or Unknown in there. If that is the case, your DB really should be enforcing data integrity, that’s what a DBMS is for. I know you say you “can’t change it”, but I don’t think this answer would be complete if I didn’t recommend against such dangerous practices.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have column with varchar values like 022008 that I need to convert into
I have column with values like this: $30K - $50K How to extract lower
In sql server database, I have column name Image which has urls like http://www.xyz.com/1009/image1.jpg
I have column that contains strings. The strings in that column look like this:
I need a select from table which does not have column that tells when
In database table I have column called options . It has type of integer
I have column OFF_SAT_COMP.LINKACCT and OFF_SAT_COMP.COUNTRY. I am trying to add the values of
I need to create trigger in SQL Server 2008 that gone insert all values
I would like to perform query in which as a result I have column
I have column with the type ntext , and I would like to convert

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.