Wednesday, 6 June 2012

Search txt file for particular matching character

This method will return the string array of match words

I do need to search the text file contain list of word per line and i want to return list of matching word from that text file.
example for “trainer”, I should get a list equivalent to [“terrain”, “trainer”, “retrain”].
    
        public static string[] GetMatchesFor(string word, string pathToWordsDatabase)
        {
            string[] Database = File.ReadAllLines(pathToWordsDatabase);
            string result = string.Empty;

            foreach (string str in Database)
            {
                bool isMatch = false;
                if (str.Length == word.Length)
                {
                    char[] chrArray = word.ToCharArray();
                    foreach (char chr in chrArray)
                    {
                        if (str.Contains(chr.ToString()))
                        {
                            isMatch = true;
                        }
                        else
                        {
                            isMatch = false;
                            break;
                        }
                    }
                    if (isMatch)
                    {
                        result = result + str + ",";
                    }
                }
                else
                {
                    continue;
                }
            }
            int count = result.Split(',').Length - 1;
            string[] resultArray = new string[count];
            for (int i = 0; i < count; i++)
            {
                resultArray[i] = result.Split(',')[i];
            }
            return resultArray;
        }

Thursday, 31 May 2012

Passing Temporary  Table as parameter to SQL Function

  1. Define your own Table Type
  2. Declare your table Variable of your own table type
  3. Insert the Record
  4. Pass the table variable name as the parameter to function
Define your own Table Type
CREATE TYPE TableType AS TABLE (LocationName VARCHAR(50))
GO 

Declare your table Variable of your own table type 
DECLARE @myTable TableType

Insert the Record
INSERT INTO @myTable(LocationName) VALUES('aaa')
SELECT * FROM @myTable
 
 
Pass the table variable name as the parameter to function 
CREATE FUNCTION Example( @TableName TableType READONLY)
RETURNS VARCHAR(50)
AS
BEGIN
    DECLARE @name VARCHAR(50)

    SELECT TOP 1 @name = LocationName FROM @TableName
    RETURN @nameEND
 
 
For More Reference Click Below Link
http://stackoverflow.com/questions/1609115/pass-table-as-parameter-into-sql-server-udf