Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Limit the characters returned in a TEXT column

Author  Topic 

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-07-30 : 18:11:32
Hello,

I have a select statement that looks like this:

 SELECT TOP 10 [Id], [Abstract] FROM NewsArticles


Abstract is a TEXT Column. I'd like to limit it to 50 characters... how would I do that.

Thanks,

-- shawn

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-30 : 18:14:22
LEFT or SUBSTRING will do.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-07-30 : 18:21:13
I get this error:

Msg 8116, Level 16, State 1, Procedure GetNewsItemAbstracts, Line 38
Argument data type text is invalid for argument 1 of left function.

Here is the code:
SELECT TOP 10 ni.[Id], LEFT(ni.[Abstract], 50)
Go to Top of Page

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-07-30 : 18:26:17
This worked:

SELECT TOP 10 ni.[Id], LEFT(CAST(ni.[Abstract] AS varchar(50)), 50)

Thanks!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-30 : 18:57:27
SELECT TOP 10 ni.[ID], SUBSTRING(ni.[Abstract], 1, 50)

Try and see which is fastest.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -