Question

I am using a Visual Studio 2012 Database project with a CLR UDF (below). For some reason, after deploying it to the database, I call the function and I get the type name returned instead of the expected result.

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlChars FlattenText(SqlChars text)
    {
        return new SqlChars(Regex.Replace(text.ToString(), @"\s+", " ", RegexOptions.Multiline));
    }
}

When I call it like this:

SELECT TOP 1 dbo.FlattenText(MyColumn) AS MyResult FROM MyTable

Here is what I get returned:

System.Data.SqlTypes.SqlChars
Was it helpful?

Solution

SqlChars inherits .ToString() from Object which simply returns the type name. Use text.ToSqlString().ToString() instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top