如何在NTEXT SQL Server列上使用LEFT?

时间:2021-12-04 07:24:34

How do you use the LEFT function (or an equivalent) on a SQL Server NTEXT column?

如何在SQL Server NTEXT列上使用LEFT函数(或等效函数)?

Basically I'm building a GridView and I just want to return the first 100 or so characters from the Description column which is NTEXT.

基本上我正在构建一个GridView,我只想返回描述列中的前100个字符,即NTEXT。

3 个解决方案

#1


8  

SELECT CAST(ntext_col AS nvarchar(100)) as ntext_substr FROM ...

SELECT CAST(ntext_col AS nvarchar(100))as ntext_substr FROM ...

[EDIT] Originally had it returning LEFT(N,100) of CAST to nvarchar(MAX), CASTing will truncate and since LEFT is wanted, that is enough.

[编辑]最初让它将CAST的LEFT(N,100)返回到nvarchar(MAX),CASTing将截断,因为LEFT是需要的,这就足够了。

#2


3  

You would have to cast it to a VARCHAR(MAX) first.

您必须首先将其强制转换为VARCHAR(MAX)。

#3


3  

You can use the SUBSTRING function, which "returns part of a character, binary, text, or image expression":

您可以使用SUBSTRING函数,该函数“返回字符,二进制,文本或图像表达式的一部分”:

SUBSTRING ( value_expression , start_expression , length_expression )

So, to select the first 100 characters from your Description NTEXT column, you would use something like the following:

因此,要从描述NTEXT列中选择前100个字符,您将使用以下内容:

SELECT SUBSTRING(Description, 1, 100) as truncatedDescription FROM MyTable;

#1


8  

SELECT CAST(ntext_col AS nvarchar(100)) as ntext_substr FROM ...

SELECT CAST(ntext_col AS nvarchar(100))as ntext_substr FROM ...

[EDIT] Originally had it returning LEFT(N,100) of CAST to nvarchar(MAX), CASTing will truncate and since LEFT is wanted, that is enough.

[编辑]最初让它将CAST的LEFT(N,100)返回到nvarchar(MAX),CASTing将截断,因为LEFT是需要的,这就足够了。

#2


3  

You would have to cast it to a VARCHAR(MAX) first.

您必须首先将其强制转换为VARCHAR(MAX)。

#3


3  

You can use the SUBSTRING function, which "returns part of a character, binary, text, or image expression":

您可以使用SUBSTRING函数,该函数“返回字符,二进制,文本或图像表达式的一部分”:

SUBSTRING ( value_expression , start_expression , length_expression )

So, to select the first 100 characters from your Description NTEXT column, you would use something like the following:

因此,要从描述NTEXT列中选择前100个字符,您将使用以下内容:

SELECT SUBSTRING(Description, 1, 100) as truncatedDescription FROM MyTable;