CREATE FUNCTION [MIG_STRING_TO_CHARS_Fn] (@string NVARCHAR(max)) 
RETURNS @chars TABLE (INDX INT, CHAR_VAL NCHAR) 
AS 
  BEGIN 
      DECLARE @count INT, 
              @total INT 

      SELECT @total = Len(@string), 
             @count = 1 

      WHILE @count <= @total 
        BEGIN 
            INSERT INTO @chars 
            SELECT @count, Substring(@string, @count, 1) 

            SELECT @count = @count + 1 
        END 

      RETURN 
  END 



