• You are totally right...

    typin' mistake...

    the 'CAST' is to a NVARCHAR(MAX)

    I tried a lot whitout that CAST and it returned another 'md5' string... i figure that the problem is in the asigment '@md5 = ...'

    the sql corrected would be...

    CREATE FUNCTION [dbo].[fn_MD5]

    (

    @string AS VARCHAR(MAX),

    @chars AS INT

    )

    RETURNS NVARCHAR(32)

    AS

    BEGIN

    -- Declare the return variable here

    DECLARE @md5 NVARCHAR(32)

    SELECT @md5 = SUBSTRING(CAST(master.dbo.fn_varbintohexstr(HashBytes('MD5',@string )) AS NVARCHAR(MAX)),3,@chars )

    -- Return the result of the function

    RETURN @md5

    END