怎么插入一个分行在SQL服务器VARCHAR/限字符串
-
09-06-2019 - |
题
我没有看到任何类似问题,关于这个主题,我必须研究这东西的,我的工作现在。以为我会后答案对于它的情况下,任何人都有同样的问题。
解决方案 2
你只是串连串并插入一个 CHAR(13)
你想要你的线断裂。
例如:
DECLARE @text NVARCHAR(100)
SET @text = 'This is line 1.' + CHAR(13) + 'This is line 2.'
SELECT @text
这个印出以下几点:
这是线1.
这是线路2。
其他提示
char(13)
是 CR
.为DOS/Windows格式 CRLF
换行,你想要的 char(13)+char(10)
, ,如:
'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'
另一种方式来做到这一点是这样的:
INSERT CRLF SELECT 'fox
jumped'
就是说,只是插入一条线打破您的询问而编写,它将添加类似的突破该数据库。这一工作在SQL服务器的管理工作室和查询分析。我相信这也将工作C#如果你使用@签串。
string str = @"INSERT CRLF SELECT 'fox
jumped'"
运行这在短程,它显示出如何断行在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))
结果是:
1号线
2号线
第3行
有多长一个空行喂?
2
什么是ASCII价值?
13
10
或者如果你更愿意指定你串在一线(几乎!) 你可以雇用 REPLACE()
像这样(有选择地使用 CHAR(13)+CHAR(10)
作为替换):
PRINT REPLACE('Line 1`Line 2`Line 3','`','
')
下一个 谷歌...
把代码从网站:
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)
好问题,从来没有这样做我自己:)
我来到这里是因为我关切的是,cr-劳动力调查,我指定在C#串未显示在SQl服务器的管理工作室查询的答复。
事实证明,他们是有的,但不是正在显示出来。
"看到"cr-劳动力调查,使用打印的发言,如:
declare @tmp varchar(500)
select @tmp = msgbody from emailssentlog where id=6769;
print @tmp
这里是C#功,前面添加一个文本行现有文本blob,delimited by CRLFs,返回一个T-SQL表达适合 INSERT
或 UPDATE
操作。它是得到了一些我们有专利中的错误处理,但一旦你rip,出,它可能是有益的--我希望如此。
/// <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("");
}
}
我想说的
concat('This is line 1.', 0xd0a, 'This is line 2.')
或
concat(N'This is line 1.', 0xd000a, N'This is line 2.')
这始终是很酷,因为当你得到出口的名单,说Oracle,然后你得到记录跨越几个线,这反过来能够有趣的,也就是说,cvs文件,所以要小心。
无论如何,抢劫的答案是好的,但是我建议采用别的东西比@,尝试一些更像§§@@§§或什么的,这样它将有机会针对一些独特性。(但还是记得的长度 varchar
/nvarchar
现场你是插入到..)