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?