問題描述
考慮這樣一個場景,您希望從表中提取最后 x 個條目.我們想要的列包含有關產品的推薦.出于性能原因,我們只想從推薦中獲取前 50 個字符.該列名為 TestimonialText,類型為 text
.
Consider a scenario where you'd like to pull the last x entries from a table. The column we want contains testimonials about a product. For performance reasons, we only want to grab the first 50 characters from the testimonial. The column is named TestimonialText and is of type text
.
考慮這個 T-SQL 的精簡片段:
Consider this condensed snippet of 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.
Argument data type text is invalid for argument 1 of left function.
問題:如何只提取 text 或 ntext 列的前幾個 n 個字符?
Question: how to extract just the first few n characters of the text or ntext column?
推薦答案
我認為 SUBSTRING 是更好的選擇.試試這個:
I think SUBSTRING would be a better choice. Try this:
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
這篇關于T-SQL:從 text 或 ntext 列中選擇前 n 個字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!