0

I am actually not good in functions in SQL, i am more a java/android programmer. But for a school project i need to check if a String is in some array in T-SQL. But i am comparing data from two different companies, and they both have some flaws in their data. The data of one of the companies is in a Database, the other from excel.

One way to do this is using the LIKE function from SQL ofcourse, but i am not very sure what the criteria are. What would be perfect is a function like the sql function IN, but that requires a perfect match...

So what i did is the following:

Create a T-SQL function that gives the Levenshtein distance , it compares two strings and gives the Levenshtein distance.

This function works good.

The next step for me is to create a function with the following parameters:

  • input string - the string to check
  • levdist int - the minimal levenstein distance to give a match
    (perfect match would be 0)
  • arr array/table/llist (cant find how a comma separated string (like you put in the IN function) is called - where to check

What i have so far is this:

    CREATE FUNCTION inArrayWithLevenstheinDist 
(
    -- Add the parameters for the function here
    @stringToCheck varchar,
    @levDist int,
    @arr TABLE ??? 
)
RETURNS Boolean
AS
BEGIN
    -- Declare the return variable here
    DECLARE @Result bool
    SET @Result = FALSE

    -- Add the T-SQL statements to compute the return value here
    DECLARE @intFlag INT
    SET @intFlag = 1
    WHILE (@intFlag <=5)
    BEGIN
    IF [100].dbo.LEVENSTEINDIST(@stringToCheck,arr(@intFlag) < @levDist
    SET @Result = TRUE
    END //end if 


    SET @intFlag = @intFlag + 1
    IF @intFlag = arr.size ??????
    BREAK;
    END //end while
    GO





    -- Return the result of the function
    RETURN @Result

END //end function
GO

This doesnt work and gives compile errors. So my two questions are (what i think are the points my code is wrong)

  • how to return a boolean value in a function (i cant find it :S )? Offcourse i can return an in integer and use 0 for false and 1 for true or so, but it should be possible

  • how to use an array/list/table like in the standard T-SQL IN-function?

Jasper
  • 2,389
  • 4
  • 25
  • 40
  • Does this help? http://stackoverflow.com/q/560709/27535 – gbn May 13 '13 at 14:37
  • not really, this is about the levenshtein function that is already working fine by me, but thanks for thinking with me – Jasper May 13 '13 at 14:39
  • TSQL has no boolean data type; `bit` is commonly used as an alternative. And you may be looking for [table-valued parameters](http://msdn.microsoft.com/en-us/library/bb510489.aspx). Please ask only one question at a time; you can ask as many as you want so there's no need to put multiple questions together. – Pondlife May 14 '13 at 16:32

0 Answers0