简体   繁体   English

MSSQL:作为文本输出时截断 nvarchar(max)

[英]MSSQL: Truncate nvarchar(max) when outputted as text

On MSSQL when I run my query I get the following output when run as Results To Text:在 MSSQL 上运行查询时,作为Results To Text:运行时得到以下输出Results To Text:

Description                                                                                                                                                                                                                                                      Another Column
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
This is a Description                                                                                                                                                                                                                                            43289

But interestingly there aren't any blanks at all at the end of the string, and the length is also the correct one (without any blanks)但有趣的是,字符串的末尾根本没有任何空格,并且长度也是正确的(没有任何空格)

I want to have an output like this:我想要这样的输出:

Description    Another Column
-------------- --------------
My description 543893                                                                                                                                                                                                                                

I've tried rtrim , ltrim , replace , but none is working.我试过rtrimltrimreplace ,但都没有工作。 The datatype is nvarchar(max) .数据类型是nvarchar(max)

As I say, if you really need to do this, you have to do two passes through the data:正如我所说,如果你真的需要这样做,你必须对数据进行两次传递:

create table #t (description nvarchar(max),otherColumn int)
insert into #t(description,otherColumn) values
(N'Short Desc.',21),
(N'Loooooonnnnnngggggg Description',99)

declare @maxLength int
declare @sql nvarchar(max)

select @maxLength = MAX(LEN(description)) from #t

if @maxLength <= 4000
begin
    set @sql = N'SELECT CONVERT(nvarchar(' + CONVERT(nvarchar(4),@maxLength) +
               N'),description) as description'
end
else
begin
    set @sql = N'SELECT description'
end
set @sql = @sql + N',otherColumn from #t'

exec sp_executesql @sql

Result:结果:

description                     otherColumn
------------------------------- -----------
Short Desc.                     21
Loooooonnnnnngggggg Description 99

Even if you use RTRIM , etc, when the system is compiling the query, it doesn't know the nature of the data contained in individual rows 1 .即使您使用RTRIM等,当系统编译查询时,它也不知道单个行1 中包含的数据的性质。 And so it can only assign the data type nvarchar(max) as the type of the resulting column.因此它只能将数据类型nvarchar(max)为结果列的类型。

In turn, when SSMS starts to receive the result set, it's informed that the column is an nvarchar(max) .反过来,当 SSMS 开始接收结果集时,它会被告知该列是nvarchar(max) At the point at which it's printing the column headers , it doesn't know the nature of the lengths of the results it will receive, so it just has to adopt a sensible default display width that can cope with any data it receives.在打印列标题时,它不知道它将接收到的结果长度的性质,因此它只需要采用一个合理的默认显示宽度来处理它接收到的任何数据。

I'd normally leave such format considerations to an actual presentation layer (eg report builder or application), rather than fiddling around and trying to force SQL/SMS to do this sort of formatting.我通常会将这种格式考虑留给实际的表示层(例如报告生成器或应用程序),而不是摆弄并试图强制 SQL/SMS 执行此类格式化。

1 Eg one of the rows might have the description containing the complete works of Shakespeare, in the original Klingon. 1例如,其中一行可能有包含莎士比亚全集的description ,原始克林贡语。

Try this尝试这个

select Description,len(rtrim(Description)) as count from table

Edit编辑

The actual problem is you set the result mode to text.实际问题是您将结果模式设置为文本。 Change it to grid mode.将其更改为网格模式。 You will get the correct result你会得到正确的结果

I know this is old, but for anyone who might need it in the future (including myself with how bad my memory is).我知道这很旧,但对于将来可能需要它的任何人(包括我自己的记忆力有多差)。

I ran into this same problem.我遇到了同样的问题。 Here was the solution I came up with:这是我想出的解决方案:

SELECT CASE 
        WHEN LEN([Description]) >= 20
            THEN CAST((LEFT([Description], 17) + '...') AS VARCHAR(20))
        ELSE CAST([Description] AS VARCHAR(20))
        END AS [Description]
    ,[Another Column]
FROM [dbo].[YourTableName];

I hope you find this helpful, even though it is a bit of a long-winded way to do it.我希望你觉得这很有帮助,尽管这样做有点冗长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM