【发布时间】:2019-04-05 14:40:52
【问题描述】:
给定以下 T-SQL 代码:
declare @lf char(1) set @lf = char(10);
declare @cr char(1) set @cr = char(13);
select replace(isnull(note,''), @cr+@lf,@lf) from T
在某些情况下,@cr+@lf 在note 列中不是每次出现都会被@lf 替换?
我正在尝试对确实发生这种情况的情况进行故障排除。
note 列定义为nvarchar(max)。 documentation 说:
如果 string_expression 不是 varchar(max) 或 nvarchar(max) 类型,REPLACE 会在 8,000 字节处截断返回值。要返回大于 8,000 字节的值,必须将 string_expression 显式转换为大值数据类型。
如果我理解正确,则无需强制转换,因为 note 已经是允许返回值大于 8000 字节的正确数据类型。
我想也许isnull 函数没有返回nvarchar(max) 但documentation 说它返回被测试值的类型:
...返回类型
返回与 check_expression 相同的类型。
并且返回的值没有被截断;只是有些crlf对被忽略了。
我一定是忽略了什么。
declare @t table( notes nvarchar(max));
insert @t(notes)
values
(
'From: kkkkkkk@aaaaaaaaa.com <kkkkkkk@aaaaaaaaa.com>
Sent: Monday, May 00, 0008 00:55 PM
To: Jan Zzzz <sZzzz@dddd.dd.com>
Subject: RE: [Secure Message] aaaaaaaaa ABC ddddddddddddd--XXXXX-X
Hi Jan,
The ddddddddddddd is valid for one year. I have attached the Zzzzzzz Rrrrrrrr which you will find behind the blank cover page and ddddddddddddd form. Please let me know if this is what you need.
Best Regards,
Yyyyyy
Kkkkkkkk Kkkkkk, ABC, DEF
ABC Mmmmmmmm
P 800.007.0000 ext 000 | F 600.000.0070
Electronic mail is not considered to be a secure form of communication for Protected Health Information. If you are concerned about privacy, please call aaaaaaaaa directly at 0-800-007-0000. If you would like to review our privacy policy, it can be found on our website: www.ddddddddddddd.com.
This email, including any attached files, may contain confidential and privileged information for the sole use of the intended recipient(s). Any review, use, distribution or disclosure by others is strictly prohibited. If you are not the addressee indicated in this message (or authorized to receive information for the recipient), please contact the sender by reply e-mail and delete all copies of this message (including any attachments).
From: Jan Zzzz <sZzzz@dddd.dd.com>
Sent: Monday, May 00, 0008 8:56 AM
To: Kkkkkkkk Kkkkkk <kkkkkkk@aaaaaaaaa.com>; Jan Zzzz <sZzzz@dddd.dd.com>
Subject: Re: [Secure Message] aaaaaaaaa ABC ddddddddddddd--XXXXX-X
Hi, this expired, I need a copy of the aaaaaaa aaaa so I can submit my aaaaaaa aaa aaaaaaaa. Thank you. SZzzz
On 0/00/0008 8:00 AM, Jan Zzzz wrote:
Thank you for the dddddddd, I am mmmmmmm mmm today.
On 0/0/0008 6:05 PM, Kkkkkkkk Kkkkkk wrote:
[Secure Message] aaaaaaaaa ABC ddddddddddddd--XXXXX-X
Kkkkkkkk Kkkkkk has sent you a secure message.
Subject: aaaaaaaaa ABC ddddddddddddd--XXXXX-X
From: Kkkkkkkk Kkkkkk <kkkkkkk@aaaaaaaaa.com>
To: Jan Zzzz <sZzzz@dddd.dd.com>
Expires: May 00, 0008
View Secure Message
Note: If you have any concerns about the authenticity of this message please contact the original sender directly.'
)
select notes from @t;
select replace(notes, char(13),'') from @t;
【问题讨论】:
-
“对确实发生这种情况的情况进行故障排除。”您能否创建一个可重现的示例?
-
@UnhandledExcepSean 我正在整理一些东西,但由于这是一个时间敏感的项目,我希望这个问题会在之前遇到过它的人身上“跳出来”。
-
提示:您可以显示表达式的数据类型:
select SQL_Variant_Property( IsNull( Note, '' ), 'BaseType' )。有关可用的其他数据,请参阅SQL_Variant_Property。 -
@HABO: 错误:“操作数类型冲突:nvarchar(max) 与 sql_variant 不兼容”但我想您需要的信息在错误中:)
-
不重现,我认为您有一个不同的问题,例如在 SSMS 中将 CR 添加到查看器或任何消耗剥离字符串的内容,或者您正在连接字符串并且仅替换一部分中的 CR。我运行它以显示 CR 已正确删除。
UPDATE @t SET notes=replace(notes, char(13),'');SELECT * FROM @t WHERE notes like '%' + CHAR(13) + '%'
标签: tsql replace sql-server-2012