IsNumeric 在 SQL Server 中不起作用
对这个简单的查询感到非常恼火......
我需要向 varchar 添加一个偏移量,如果它是一个数字并且不执行任何操作,否则它不是。
为此,我在 SQL 服务器中创建了以下函数。 然后我用以下方法提取答案: select dbo.OffsetKPL("100",200)
但是这不起作用,我收到错误
消息 207,第 16 级,状态 1,第 1 行
列名“100”无效。
该函数的代码如下...
ALTER FUNCTION [dbo].[OffsetKPL](
@kpl varchar(20)
,@offset int = 0
)
RETURNS varchar(20)
AS
BEGIN
DECLARE @uitkomst varchar(20);
set @uitkomst = @kpl;
if not(@offset = 0) begin
if (IsNumeric(@uitkomst) = 1) begin
set @uitkomst = cast((cast(@kpl as int) + @offset) as varchar);
end;
end;
RETURN @uitkomst;
END
有什么问题吗?它没有任何地方声明 IsNumeric 不接受变量。
Getting very annoyed with this simple query...
I need to add an offset to a varchar, if it's a number and do nothing is it is not.
For this reason I've created the following function in SQL-server.
I then extract the answer with:select dbo.OffsetKPL("100",200)
However this does not work, I get the error
Msg 207, Level 16, State 1, Line 1
Invalid column name '100'.
The code for the function is as follows...
ALTER FUNCTION [dbo].[OffsetKPL](
@kpl varchar(20)
,@offset int = 0
)
RETURNS varchar(20)
AS
BEGIN
DECLARE @uitkomst varchar(20);
set @uitkomst = @kpl;
if not(@offset = 0) begin
if (IsNumeric(@uitkomst) = 1) begin
set @uitkomst = cast((cast(@kpl as int) + @offset) as varchar);
end;
end;
RETURN @uitkomst;
END
What's wrong? nowhere does it state that IsNumeric does not accept a variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对字符串使用单引号!
如果你有
QUOTED_IDENTIFIER
(默认),双引号中的内容应该是对象名称。isnumeric
可能不是您所需要的,因为各种意外的事情都会为此返回1
。请参阅 IsNumeric() 损坏了吗?仅就这一点进行一些讨论。
Use single quotes for strings!
If you have
QUOTED_IDENTIFIER
on (the default) things in double quotes are expected to be object names.isnumeric
may not be what you need though as all kinds of unexpected things return1
for this.See IsNumeric() Broken? Only up to a point for some discussion on this point.