3

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.

Johan
  • 74,508
  • 24
  • 191
  • 319

1 Answers1

7

Use single quotes for strings!

select dbo.OffsetKPL('100',200)

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 return 1 for this.

SELECT ISNUMERIC('$'), ISNUMERIC('.'), 
       ISNUMERIC('12d5'), ISNUMERIC(','), ISNUMERIC('1e1')

See IsNumeric() Broken? Only up to a point for some discussion on this point.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • Yep that works, I was really stuckenstein for a while there, anyway that's what I get for taking off my tinfoil hat. – Johan Mar 24 '11 at 12:59
  • 1
    I know about the borken state of IsNumeric, but for the purpose I need it for I suffices never thought about the " issues, usually work in MySQL. – Johan Mar 24 '11 at 13:15