Mar*_*ski 530 sql sql-server line-breaks
我没有看到关于这个主题的任何类似的问题,我不得不研究这个我现在正在做的事情.以为我会发布答案,以防其他人有同样的问题.
Sör*_*lau 570
char(13)
是CR
.对于DOS-/Windows风格的CRLF
换行符,您需要char(13)+char(10)
,如:
'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'
Run Code Online (Sandbox Code Playgroud)
Mar*_*ski 268
我在这里找到了答案: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
Run Code Online (Sandbox Code Playgroud)
这打印出以下内容:
这是第1行.
这是第2行.
Fra*_*k V 80
另一种方法是这样的:
INSERT CRLF SELECT 'fox
jumped'
Run Code Online (Sandbox Code Playgroud)
也就是说,只需在编写查询时在查询中插入换行符,就会向数据库添加类似的中断.这适用于SQL Server Management Studio和Query Analyzer.我相信如果你在字符串上使用@符号,这也适用于C#.
string str = @"INSERT CRLF SELECT 'fox
jumped'"
Run Code Online (Sandbox Code Playgroud)
AjV*_*Jsy 21
在SSMS中运行它,它显示SQL本身的换行符如何成为跨越行的字符串值的一部分:
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))
Run Code Online (Sandbox Code Playgroud)
结果:
第1
行第2
行第3行
空白行有多长时间了?
2
什么是ASCII值?
13
10
或者,如果你想在一行(几乎!)上指定你的字符串,你可以REPLACE()
这样使用(可选择CHAR(13)+CHAR(10)
用作替换):
PRINT REPLACE('Line 1`Line 2`Line 3','`','
')
Run Code Online (Sandbox Code Playgroud)
Tru*_*ubs 21
所有这些选项都适用于您的情况,但如果您使用 SSMS,您可能看不到它们中的任何一个 (如某些评论中提到的 SSMS 隐藏 CR/LF)
因此,与其驾驶自己绕过弯道,不如检查此设置
Tools
|
Options
Rob*_*per 15
关注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))
Run Code Online (Sandbox Code Playgroud)
看起来可以通过用CHAR替换占位符来完成(13)
好问题,从来没有自己做过:)
小智 12
我来到这里是因为我担心我在C#字符串中指定的cr-lfs没有在SQl Server Management Studio查询响应中显示.
事实证明,他们在那里,但没有被展示.
要"看到"cr-lfs,请使用如下的print语句:
declare @tmp varchar(500)
select @tmp = msgbody from emailssentlog where id=6769;
print @tmp
Run Code Online (Sandbox Code Playgroud)
我会说
concat('This is line 1.', 0xd0a, 'This is line 2.')
Run Code Online (Sandbox Code Playgroud)
或者
concat(N'This is line 1.', 0xd000a, N'This is line 2.')
Run Code Online (Sandbox Code Playgroud)
小智 5
这是一个 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("");
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
937485 次 |
最近记录: |