Sql – Returning non-printable sybol (enter) to SQL Server from C# via CLR

clrsql-serversql-server-2005

I have the following CLR function:

    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString PrintText(SqlString text)
    {
        // Put your code here
        return new SqlString(text.Value);
    }

And I want to get an enter symbol when passing

'\r\n'

to it. But insteat I get

'\r\n'

Could you please tell me what's wrong with this code.

Thank you.

Best Solution

In T-SQL you don't write a line break as \r\n. Instead you just use a line break:

'this
is a
string
in SQL
with
line breaks'

If you pass a string with \r\n to the C# code, nothing magical happens, it doesn't automatically get converted. The backslash character is just a character like any other. It's when you use the backslash in a literal string in the code that the compiler uses it as an escape code, and puts the control characters in the actual string.