Sql server T-SQL:从文本或ntext列中选择前n个字符

Sql server T-SQL:从文本或ntext列中选择前n个字符,sql-server,tsql,Sql Server,Tsql,考虑一个场景,其中您希望从表中提取最后的x项。我们想要的专栏包含关于产品的推荐。出于性能方面的考虑,我们只想从推荐信中抓取前50个字符。该列名为推荐文本,类型为text 考虑一下这段浓缩的T-SQL代码片段: SELECT TOP 10 C.FirstName + ' ' + C.LastName AS CustomerName ,LEFT(C.TestimonialText,50) AS TestimonialSnippet ,C.TestimonialDate FR

考虑一个场景,其中您希望从表中提取最后的x项。我们想要的专栏包含关于产品的推荐。出于性能方面的考虑,我们只想从推荐信中抓取前50个字符。该列名为推荐文本,类型为
text

考虑一下这段浓缩的T-SQL代码片段:

SELECT TOP 10
    C.FirstName + ' ' + C.LastName AS CustomerName
    ,LEFT(C.TestimonialText,50) AS TestimonialSnippet
    ,C.TestimonialDate

FROM Customer AS C  
ORDER BY C.TestimonialDate DESC
这会产生一个错误:

参数数据类型文本对于无效 左函数的参数1


问题:如何仅提取文本或ntext列的前几个n个字符?

如果使用SQL Server 2005或更高版本,请不要使用文本数据类型,因为它是无润滑的。使用varchar(最大值)或nvarchar(最大值)。所有字符串函数都可以工作。在这里阅读更多信息:

您正在寻找类似的产品吗?注意SELECT语句中的强制转换(C.TEXT为VARCHAR(50))

SELECT TOP 10
    C.FirstName + ' ' + C.LastName AS CustomerName,
    CAST(C.TestimonialText AS VARCHAR(50)) AS TestimonialSnippet,
    C.TestimonialDate
FROM Customer AS C  
ORDER BY C.TestimonialDate DESC
以下是一些测试数据

测试数据设置

结果


我认为子字符串是一个更好的选择。试试这个:

SELECT TOP 10
    C.FirstName + ' ' + C.LastName AS CustomerName
    ,SUBSTRING(C.TestimonialText,1,50) AS TestimonialSnippet
    ,C.TestimonialDate
FROM Customer AS C  
ORDER BY SUBSTRING(C.TestimonialText,1,50) DESC

基本上,发生的事情是,您为LEFT函数的第一个参数提供了无效的数据类型。确保您将文本数据类型转换为varchar或nvarchar,那么您的查询肯定可以工作。下面是我在SQLServer2005中测试的一个示例

Create table #Customer

(
 firstName varchar(30)
 ,lastName varchar(30)
 ,testimonial text
 ,testimonialDate DateTime

)

GO

INSERT INTO #Customer (firstName, lastName, testimonial, testimonialDate ) VALUES('Jonhn', 'Smith', 'we really really like your product and blaha ......', getDate())
GO
INSERT INTO #Customer (firstName, lastName, testimonial , testimonialDate) VALUES('Mary', 'Toe', 'we really really like your product and blaha ......', getDate() - 3)
GO
INSERT INTO #Customer (firstName, lastName, testimonial , testimonialDate) VALUES('Amanda', 'Palin', 'we really really like your product and blaha ......', getDate() -2 )
GO

SELECT TOP 3    C.FirstName + ' ' + C.LastName AS CustomerName    ,LEFT( CAST(C.Testimonial as varchar(50)),50) AS TestimonialSnippet    ,C.TestimonialDate FROM #Customer AS C  ORDER BY C.TestimonialDate DESC
GO
Drop table #Customer
GO

谢谢你,小鸟<如果您不想强制转换为
varchar
,则可以使用code>SUBSTRING方法。谢谢Sergey,感谢您对去除擦痕数据类型的说明。
SELECT TOP 10
    C.FirstName + ' ' + C.LastName AS CustomerName
    ,SUBSTRING(C.TestimonialText,1,50) AS TestimonialSnippet
    ,C.TestimonialDate
FROM Customer AS C  
ORDER BY SUBSTRING(C.TestimonialText,1,50) DESC
Create table #Customer

(
 firstName varchar(30)
 ,lastName varchar(30)
 ,testimonial text
 ,testimonialDate DateTime

)

GO

INSERT INTO #Customer (firstName, lastName, testimonial, testimonialDate ) VALUES('Jonhn', 'Smith', 'we really really like your product and blaha ......', getDate())
GO
INSERT INTO #Customer (firstName, lastName, testimonial , testimonialDate) VALUES('Mary', 'Toe', 'we really really like your product and blaha ......', getDate() - 3)
GO
INSERT INTO #Customer (firstName, lastName, testimonial , testimonialDate) VALUES('Amanda', 'Palin', 'we really really like your product and blaha ......', getDate() -2 )
GO

SELECT TOP 3    C.FirstName + ' ' + C.LastName AS CustomerName    ,LEFT( CAST(C.Testimonial as varchar(50)),50) AS TestimonialSnippet    ,C.TestimonialDate FROM #Customer AS C  ORDER BY C.TestimonialDate DESC
GO
Drop table #Customer
GO