如何过滤大于数值的nvarchar?
我有一个 MS SQL 表 McTable,其中包含 BigMacs nvarchar(255) 列。 我想获取 BigMacs 值大于 5 的行。
我所做的是:
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
结果我收到错误:
状态 1,第 67 行转换失败 转换 nvarchar 值时 '***' 到 数据类型 int。
但是当我删除最后一个过滤器 where Cast(table.BigMacsL as int) > 5
它有效,我得到了这个结果:
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
当我将第一个查询“select * from”更改为“select top 18 * from”时,我也没有收到错误!
我不知道问题是什么以及如何让它发挥作用! 请你帮助我好吗?
再说一次:我在这里尝试完成的是获取 BigMacs 值大于 5 的这些 McTable 行。
更新
重现此错误的步骤:
我已经准备好查询,以便您可以轻松地在数据库上收到此错误:
创建数据库 TestDB,创建表:
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]
插入值:
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')
此查询:
select
CAST(MyVal as int) MyValInt
from
dbo.TestTable
where
IsNumeric(MyVal) = 1
产生有效数字,如下所示:
54543534
54534534
454
当您尝试使用此查询获取过滤值时:
select
*
from
(
select
CAST(MyVal as int) MyValInt
from
dbo.TestTable
where
IsNumeric(MyVal) = 1
) tabela
where
tabela.MyValInt > 6
您应该收到不应发生的错误:
Msg 245,Level 16,State 1,Line 1 将 nvarchar 值“fd”转换为数据类型 int 时转换失败。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
发生的事情是这样的:谓词
BigMacs Like '%[0-9]%'
并不完全符合您的想法。 它选择字符串中至少有一位数字的行。这不是你想要的。 您想要仅包含数字的字符串。 不幸的是,LIKE 通配符并没有为我们提供一种简单的方法来要求这一点。
我们可能足够接近您的问题。 如果我们要求,
我们将过滤掉大多数包含但数字的行。 “大多数”,因为我们的 like 通配符不包含所有可能的非数字字符。 反过来,这应该能让演员阵容发挥作用。
所以:
Here's what's happening: the predicate
BigMacs Like '%[0-9]%'
doesn't quite do what you think. It selects rows that have at least one digit somewhere in the string.This is not what you want. You want strings that only have digits. Unfortunately, the LIKE wilcards don't give us an easy way to ask for that.
We may come close enough for your problem. If we ask for
we'll filter out most rows that have anything but numbers. "Most", because our like wildcard doesn't contain all possible non-numeric characters. This, in turn, should allow the cast to work.
So:
这似乎给出了预期的结果
编辑
并且包括您的where子句
This seems to give the expected results
edit
and includes your where clause
我认为 David M. 已经把它钉在了头上,但对于那些要求重现该问题的脚本的人来说:
I think that David M. has nailed it on the head, but for those who asked for a script that reproduces the problem:
我认为使用 ISNUMERIC() 函数也有帮助。
例子:
I think using of ISNUMERIC() function also could help.
Example:
该代码应该可以工作:
我认为原始查询失败的原因可能与 SQL 评估查询的顺序有关。
That code should work:
I think that the reason the original query fails may be related to the order in which the query is evaluated by SQL.
您修改后的脚本的新答案。 发生的情况是 SQL Server 查询优化器正在优化您的子查询。 它对测试表执行一次扫描,并将内部和外部 WHERE 子句合并为一个。 这就是为什么你仍然收到错误的原因。 要查看这一点,请查看查询的估计执行计划,并将鼠标悬停在“聚集索引扫描”图标上以查看实际执行的内容。 您将看到扫描上应用以下谓词:
因此,无论查询的结构如何,它都会尝试对表中的每一行执行 CAST/CONVERT...
要避免这种情况,请使用表变量或临时表无法优化:
您实际想要返回的结果集会有所不同,因此我建议将主键与 int 值一起存储在表变量中,然后将最终查询作为这样的联接进行:
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:
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:
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:
您获得此值是因为
BigMacs.BigMac
中的某些值包含非数字值。 在你的情况下“***”。这是因为首先返回的至少 18 行具有数字
BigMacs.BigMac
值。创建一个名为 isReallyNumeric(),它解决什么是真正的数字或不是数字。
使用 isReallyNumeric() 函数仅过滤掉数字 BigMac
我还优化了查询,使用 CTE(通用表表达式)将 BigMacs.BigMac 转换为整数一次。
You are getting this value since some of the values in
BigMacs.BigMac
contains a non-numeric value. In your case "***".It is because at least first returned 18 rows have numeric
BigMacs.BigMac
values.Create a new User-Defined method called isReallyNumeric(), which addresses what is really numeric or not.
Filter out only numeric BigMac using isReallyNumeric() function
I have also optimized the query to cast
BigMacs.BigMac
into integer once using CTE (Common Table Expression).问题是,只有当某个值确实包含 int 时,您才能将它强制转换为 int。 显然你的前 18 行就是这样。 但是,如果您包含更多行,它会到达无法将值转换为 int 的行,并且您会收到您所描述的错误。 怎么样:
这将找到该列文本中包含大于 5 的数字的所有行(假设不包含小数或负数)。
The problem is that you can only CAST a value to an int if it does indeed contain an int. Clearly your first 18 rows do. But then if you include more rows, it reaches a row where the value cannot be cast to an int and you get the error you describe. How about this:
That will find all rows containing a number within the text of this column that is greater than 5 (assuming there are no decimals or negative numbers contained).
当将字符数据和数字存储在同一列中时,isnumeric 并不总是有效。 它也不限于可以转换为整数的项目。 请参阅此链接以获取解释:
http://www.tek-tips.com/faqs.cfm?fid= 6423
我的第一个问题是为什么你要把你想要使用的数字和字符存储在同一列中? 这是一个严重的设计缺陷,如果可能的话应该予以纠正。
我相信该链接可能会帮助您弄清楚如果您无法更改结构该怎么办。
OK isnumeric doesn't always work when storing character data and numbers in the same column. Nor is it limited to items which can be converted to integers. See this link for an explanation:
http://www.tek-tips.com/faqs.cfm?fid=6423
My first question is why are you storing things things you want to use as numbers and characters in the same column? This is a severe design flaw and should be corrected if at all possible.
I believe the link might help you figure out what do do if you cannot change the structure.