如何在SQL Server VARCHAR / NVARCHARstring中插入换行符

我没有看到有关这个主题的任何类似的问题,我不得不研究这个我正在做的事情。 以为我会张贴答案,以防其他人有相同的问题。

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

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

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

你只要连接string,并插入一个CHAR(13)你想要你的换行符。

例:

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

这将打印出以下内容:

这是第一行。
这是第2行。

另一种做法是这样的:

 INSERT CRLF SELECT 'fox jumped' 

也就是说,只要在写入时在查询中插入一个换行符就会将类似break的内容添加到数据库中。 这可以在SQL Server Management Studio和Query Analyzer中使用。 我相信如果您在string上使用@符号,这也将在C#中起作用。

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

继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)) 

看起来可以通过用CHARreplace占位符(13)

好问题,从来没有做过自己:)

我来到这里是因为担心我在C#string中指定的cr-lfs没有显示在SQl Server Management Studio查询响应中。

事实certificate,他们在那里,但没有被显示。

要“看”cr-lfs,请使用print语句,如:

 declare @tmp varchar(500) select @tmp = msgbody from emailssentlog where id=6769; print @tmp 

在SSMS中运行它,它显示了SQL本身中的换行如何成为跨越行的string值的一部分:

 PRINT 'Line 1 Line 2 Line 3' PRINT '' PRINT 'How long is a blank line feed?' PRINT LEN(' ') PRINT '' PRINT 'What are the ASCII values?' PRINT ASCII(SUBSTRING(' ',1,1)) PRINT ASCII(SUBSTRING(' ',2,1)) 

结果:
1号线
2号线
3号线

换行空白多久?
2

什么是ASCII值?
13
10

或者如果你宁愿在一行上指定你的string(几乎!),你可以像这样使用REPLACE() (可选地使用CHAR(13)+CHAR(10)作为replace):

 PRINT REPLACE('Line 1`Line 2`Line 3','`',' ') 

下面是一个C#函数,它将文本行预先添加到由CRLFs分隔的现有文本Blob中,并返回适用于INSERTUPDATE操作的T-SQLexpression式。 它有我们的一些专有的error handling,但一旦你把它解开,这可能是有益的 – 我希望如此。


  /// <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(""); } } 

这总是很酷,因为当你从Oracle那里得到导出的列表时,那么你得到的logging跨越了几行,这反过来对于CVS文件来说可能是有趣的,所以要小心。

无论如何,罗布的答案是好的,但我会build议使用比@更多的东西,再尝试一些东西,比如§§@@§§什么的,所以它有一定的独特性。 (但是,请记住插入到的varchar / nvarchar字段的长度..)