关于这个话题,我没有看到任何类似的问题,我必须为我现在正在做的事情研究这个问题。我想我应该把答案贴出来以防其他人也有同样的问题。
当前回答
char(13)是CR。对于DOS-/ windows风格的CRLF换行符,你需要char(13)+char(10),像这样:
'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'
其他回答
在某些特殊情况下,你可能会发现这很有用(例如在MS Report中呈现cell-content) 例子:
select * from
(
values
('use STAGING'),
('go'),
('EXEC sp_MSforeachtable
@command1=''select ''''?'''' as tablename,count(1) as anzahl from ? having count(1) = 0''')
) as t([Copy_and_execute_this_statement])
go
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字段的长度..)
下面是一个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("");
}
}
跟着谷歌…
从网站上获取代码:
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)替换占位符来完成。
问得好,我自己从来没做过:)
推荐文章
- 如何从一个查询插入多行使用雄辩/流利
- 如何连接列在Postgres选择?
- 有人可以对SQL查询进行版权保护吗?
- 如何知道MySQL表最近一次更新?
- 如何转储一些SQLite3表的数据?
- 如何检查SQL Server文本列是否为空?
- 如何创建一个SQL Server函数“连接”多行从一个子查询到一个单独的分隔字段?
- 在MySQL中的一个查询中更新多个具有不同值的行
- 在SQL中更新多个列
- 如何删除表中特定列的第一个字符?
- MySQL OR与IN性能
- 哪个更快/最好?SELECT *或SELECT columnn1, colum2, column3等
- GROUP BY with MAX(DATE)
- 删除id与其他表不匹配的sql行
- 等价的限制和偏移SQL Server?