I have an MS SQL McTable table with a BigMacs nvarchar (255) column. I would like to get rows with a BigMacs value greater than 5.
What am I doing:
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 as a result, I get an error message:
State 1, line 67 Conversion failed while converting nvarchar value "***" - int data type.
But when I remove the last filter where Cast(table.BigMacsL as int) > 5 , it works, and I get this result:
6 0x3600000000000000000000000000000000000000000000000000000000000000 6
23 0x3200330000000000000000000000000000000000000000000000000000000000 23
22 0x3200320000000000000000000000000000000000000000000000000000000000 22
24 0x32003400000000000000000000000000000000000000000000000000000000 24
25 0x3200350000000000000000000000000000000000000000000000000000000000 25
3 0x3300000000000000000000000000000000000000000000000000000000000000 3
17 0x3100370000000000000000000000000000000000000000000000000000000000 17
17 0x3100370000000000000000000000000000000000000000000000000000000000 17
19 0x31003900000000000000000000000000000000000000000000000000000000 19
20 0x320030000000000000000000000000000000000000000000000000000000 20
659 0x36003500390000000000000000000000000000000000000000000000000000 659
1 0x3100000000000000000000000000000000000000000000000000000000000000 1
43 0x3400330000000000000000000000000000000000000000000000000000000000 43
44 0x34003400000000000000000000000000000000000000000000000000000000 44
45 0x3400350000000000000000000000000000000000000000000000000000000000 45
46 0x3400360000000000000000000000000000000000000000000000000000000000 46
47 0x34003700000000000000000000000000000000000000000000000000000000 47
44 0x34003400000000000000000000000000000000000000000000000000000000 44
44 0x34003400000000000000000000000000000000000000000000000000000000 44
47 0x34003700000000000000000000000000000000000000000000000000000000 47
43 0x3400330000000000000000000000000000000000000000000000000000000000 43
50 0x3500300000000000000000000000000000000000000000000000000000000000 50
44 0x34003400000000000000000000000000000000000000000000000000000000 44
And when I change in the first query 'select * from' to 'select top 18 * from', I also do not get an error!
I donβt know what the problem is and how to do it! Could you help me?
Once again: what I'm trying to do here is get these McTable strings with BigMacs greater than 5.
UPDATE
Steps to reproduce this error:
I prepared the queries, so you can easily get this error in your database:
Create a database TestDB, create a 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]
enter values ββusing:
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 request:
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 the 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, which should not occur:
Msg 245, Level 16, State 1, Line 1 Conversion error when converting nvarchar 'fd' value to int data type.