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

  • Home
  • SEARCH
  • 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 176113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:49:08+00:00 2026-05-11T13:49:08+00:00

I have a MS SQL table McTable with column BigMacs nvarchar(255). I would like

  • 0

I have a MS SQL table McTable with column BigMacs nvarchar(255). I would like to get rows with BigMacs value greater than 5.

What I do is:

select * from   (     select        BigMacs BigMacsS,        CAST(BigMacs as Binary) BigMacsB,        CAST(BigMacs as int) BigMacsL     from        McTable     where        BigMacs Like '%[0-9]%'   ) table where    Cast(table.BigMacsL as int) > 5  

And in result I get an error:

State 1, Line 67 Conversion failed when converting the nvarchar value ‘***’ to data type int.

But when I remove last filter where Cast(table.BigMacsL as int) > 5 it works and I get this result:

 6    0x360000000000000000000000000000000000000000000000000000000000 6 23   0x320033000000000000000000000000000000000000000000000000000000 23 22   0x320032000000000000000000000000000000000000000000000000000000 22 24   0x320034000000000000000000000000000000000000000000000000000000 24 25   0x320035000000000000000000000000000000000000000000000000000000 25 3    0x330000000000000000000000000000000000000000000000000000000000 3 17   0x310037000000000000000000000000000000000000000000000000000000 17 17   0x310037000000000000000000000000000000000000000000000000000000 17 19   0x310039000000000000000000000000000000000000000000000000000000 19 20   0x320030000000000000000000000000000000000000000000000000000000 20 659  0x360035003900000000000000000000000000000000000000000000000000 659 1    0x310000000000000000000000000000000000000000000000000000000000 1 43   0x340033000000000000000000000000000000000000000000000000000000 43 44   0x340034000000000000000000000000000000000000000000000000000000 44 45   0x340035000000000000000000000000000000000000000000000000000000 45 46   0x340036000000000000000000000000000000000000000000000000000000 46 47   0x340037000000000000000000000000000000000000000000000000000000 47 44   0x340034000000000000000000000000000000000000000000000000000000 44 44   0x340034000000000000000000000000000000000000000000000000000000 44 47   0x340037000000000000000000000000000000000000000000000000000000 47 43   0x340033000000000000000000000000000000000000000000000000000000 43 50   0x350030000000000000000000000000000000000000000000000000000000 50 44   0x340034000000000000000000000000000000000000000000000000000000 44 

And when I change in first query ‘select * from’ to ‘select top 18 * from’ than I do not get error too!

I don’t know what is the problem and how to make it work! Could you please help me?

Once again: what I try to accomplish here is to get these McTable rows that have BigMacs value greater than 5.

UPDATE

Steps to reproduce this error:

I’ve prepared queries so You can easily get this error on your database:

Create database TestDB, create table with:

USE [TestDB] GO /****** Object:  Table [dbo].[TestTable]    Script Date: 04/08/2009 16:27:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[TestTable](     [ID] [int] IDENTITY(1,1) NOT NULL,     [MyVal] [nvarchar](255) COLLATE Polish_CI_AS NOT NULL,  CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED  (     [ID] ASC )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] 

insert values with:

delete from TestDB.dbo.TestTable insert into TestDB.dbo.TestTable (MyVal) values ('fd') insert into TestDB.dbo.TestTable (MyVal) values ('54543534') insert into TestDB.dbo.TestTable (MyVal) values ('fat3tv3gv5') insert into TestDB.dbo.TestTable (MyVal) values ('fdf4v43 4v434v') insert into TestDB.dbo.TestTable (MyVal) values (' g dfg dfg df') insert into TestDB.dbo.TestTable (MyVal) values ('f sd 4t4gsdf') insert into TestDB.dbo.TestTable (MyVal) values ('f df 4 trwefg') insert into TestDB.dbo.TestTable (MyVal) values ('f sd f4  fgsfg sd') insert into TestDB.dbo.TestTable (MyVal) values ('54534534') insert into TestDB.dbo.TestTable (MyVal) values ('454') 

This query:

    select          CAST(MyVal as int) MyValInt      from          dbo.TestTable      where          IsNumeric(MyVal) = 1 

results in valid numbers as shown below:

54543534

54534534

454

And when you try to get filtered values with this query:

select      *  from     (         select              CAST(MyVal as int) MyValInt          from              dbo.TestTable          where              IsNumeric(MyVal) = 1     ) tabela where      tabela.MyValInt > 6 

You should get this error that should not occur:

Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the nvarchar value ‘fd’ to data type int.

  • 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. 2026-05-11T13:49:08+00:00Added an answer on May 11, 2026 at 1:49 pm

    New answer for your revised scripts. What is happening is that the SQL Server query optimiser is optimising out your subquery. It is performing a single scan of the test table, and combining the inner and outer WHERE clauses into one. That’s why you get the error still. To see this, view the estimated execution plan for the query, and hover over the Clustered Index Scan icon to see what is actually being carried out. You will see the following predicate being applied on the scan:

    CONVERT(int,[testdb].[dbo].[TestTable].[MyVal],0)>(6) AND isnumeric(CONVERT_IMPLICIT(varchar(510),     [testdb].[dbo].[TestTable].[MyVal],0))=(1) 

    So regardless of the structure of your query, it is trying to do the CAST/CONVERT on every row in the table…

    To avoid this, use a table variable or temporary table that can’t be optimised out:

    DECLARE @integers table (     MyValInt int )  INSERT INTO    @integers SELECT  CAST(MyVal AS int) FROM    dbo.TestTable  WHERE   ISNUMERIC(MyVal) = 1  SELECT  * FROM    @integers WHERE   MyValInt > 6 

    The results set you actually want to return will be different, so I’d suggest storing the primary key along with the int value in the table variable, and then doing your final query as a join like this:

    DECLARE @integers table (     ID int,     MyValInt int )  INSERT INTO    @integers SELECT  ID, CAST(MyVal AS int) FROM    dbo.TestTable  WHERE   ISNUMERIC(MyVal) = 1  SELECT  b.* FROM    @integers t         INNER JOIN                 TestTable b                 ON b.ID = t.ID WHERE   t.MyValInt > 6 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 224k
  • Answers 224k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This tends to happen if your database tables have a… May 13, 2026 at 12:46 am
  • Editorial Team
    Editorial Team added an answer The effective way to do this is to create an… May 13, 2026 at 12:46 am
  • Editorial Team
    Editorial Team added an answer As you can see I added a timeout which delay… May 13, 2026 at 12:46 am

Related Questions

I have a nvarchar column BigMacs in table McTable in my MS SQL 2005
I'm currently trying to run a LINQ query over a MS SQL database. This
I currently have a table with a column as varchar . This column can
I have a java web application that selects one column from table (with 6

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.