如何在SQL Server VARCHAR / NVARCHAR字符串中插入换行符

时间:2020-03-05 18:44:04  来源:igfitidea点击:

我没有看到与此主题相关的任何类似问题,因此我不得不针对当前正在研究的内容进行研究。我以为如果其他人有相同的问题,我会给出答案。

解决方案

回答

我在这里找到了答案:http://blog.sqlauthority.com/2007/08/22/sql-server-t-sql-script-to-insert-carriage-return-and-new-line-feed-in-代码/

我们只需将字符串连接起来,然后在要换行的位置插入CHAR(13)

例子:

DECLARE @text NVARCHAR(100)
SET @text = 'This is line 1.' + CHAR(13) + 'This is line 2.'
SELECT @text

打印出以下内容:

This is line 1.

  This is line 2.

回答

追踪Google ...

从网站获取代码:

CREATE TABLE CRLF
    (
        col1 VARCHAR(1000)
    )

INSERT CRLF SELECT 'The quick brown@'
INSERT CRLF SELECT 'fox @jumped'
INSERT CRLF SELECT '@over the '
INSERT CRLF SELECT 'log@'

SELECT col1 FROM CRLF

Returns:

col1
-----------------
The quick brown@
fox @jumped
@over the
log@

(4 row(s) affected)

UPDATE CRLF
SET col1 = REPLACE(col1, '@', CHAR(13))

看起来可以通过用CHAR(13)替换占位符来完成

好问题,我自己没做过:)

回答

char(13)是CR。对于DOS / Windows风格的CRLF换行符,我们需要char(13)+ char(10),例如:

'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'

回答

这总是很酷的,因为从Oracle这样导出列表时,我们会得到跨越几行的记录,这反过来对于cvs文件可能会很有趣,因此请当心。

无论如何,Rob的答案很好,但是我建议我们使用@以外的其他方式,再尝试一些,例如@@或者其他方式,这样它就有机会获得一些独特性。 (但仍然要记住我们要插入的varchar/nvarchar字段的长度。)

回答

这样的另一种方法是这样的:

INSERT CRLF SELECT 'fox 
jumped'

也就是说,在编写查询时只需在查询中插入一个换行符即可将类似的换行符添加到数据库中。这适用于SQL Server Management Studio和查询分析器。我相信如果在字符串上使用@符号,这也将在Cif中起作用。

string str = @"INSERT CRLF SELECT 'fox 
    jumped'"

回答

这是一个C函数,它将文本行添加到现有文本blob(由CRLF分隔)的前面,并返回适合于" INSERT"或者" UPDATE"操作的T-SQL表达式。它包含一些我们专有的错误处理,但是一旦我们将其删除,它可能会有所帮助-我希望如此。

/// <summary>
    /// Generate a SQL string value expression suitable for INSERT/UPDATE operations that prepends
    /// the specified line to an existing block of text, assumed to have \r\n delimiters, and
    /// truncate at a maximum length.
    /// </summary>
    /// <param name="sNewLine">Single text line to be prepended to existing text</param>
    /// <param name="sOrigLines">Current text value; assumed to be CRLF-delimited</param>
    /// <param name="iMaxLen">Integer field length</param>
    /// <returns>String: SQL string expression suitable for INSERT/UPDATE operations.  Empty on error.</returns>
    private string PrependCommentLine(string sNewLine, String sOrigLines, int iMaxLen)
    {
        String fn = MethodBase.GetCurrentMethod().Name;

        try
        {
            String [] line_array = sOrigLines.Split("\r\n".ToCharArray());
            List<string> orig_lines = new List<string>();
            foreach(String orig_line in line_array) 
            { 
                if (!String.IsNullOrEmpty(orig_line))  
                {  
                    orig_lines.Add(orig_line);    
                }
            } // end foreach(original line)

            String final_comments = "'" + sNewLine + "' + CHAR(13) + CHAR(10) ";
            int cum_length = sNewLine.Length + 2;
            foreach(String orig_line in orig_lines)
            {
                String curline = orig_line;
                if (cum_length >= iMaxLen) break;                // stop appending if we're already over
                if ((cum_length+orig_line.Length+2)>=iMaxLen)    // If this one will push us over, truncate and warn:
                {
                    Util.HandleAppErr(this, fn, "Truncating comments: " + orig_line);
                    curline = orig_line.Substring(0, iMaxLen - (cum_length + 3));
                }
                final_comments += " + '" + curline + "' + CHAR(13) + CHAR(10) \r\n";
                cum_length += orig_line.Length + 2;
            } // end foreach(second pass on original lines)

            return(final_comments);

        } // end main try()
        catch(Exception exc)
        {
            Util.HandleExc(this,fn,exc);
            return("");
        }
    }