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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 sql lastindexof???

Author  Topic 

age
Starting Member

1 Post

Posted - 2011-02-09 : 20:43:17
Hi all

i'm writing a stored proc and if a field i return is longer then 200 characters, i want to truncate, find the last space, remove any characters after that space, then add some full stops

eg:

INSERT INTO @rec(articleid, abstract)
SELECT a.id,
CASE
WHEN LEN(t.abstract) <= 200 THEN t.abstract
WHEN LEN(t.abstract) > 200 THEN //** HERE I WANT TO TRUNCATE, FIND LAST SPACE AND ADD ... TO THE END
ELSE ISNULL(t.abstract,'')
END
FROM [dbo].net_articles a WITH (NOLOCK).. blah blah

something similar to using substring and lastindexof methods in C# or javascript if you know what i mean

Cheers!!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-09 : 20:47:09
Use the LEFT or SUBSTRING functions.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-09 : 20:51:04
CASE
WHEN LEN(t.abstract) > 200 THEN LEFT(t.abstract,200) + '...'
ELSE ISNULL(t.abstract,'')
END



Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-09 : 20:51:19
quote:
Originally posted by dataguru1971

CASE
WHEN LEN(t.abstract) > 200 THEN LEFT(t.abstract,199) + '...'
ELSE ISNULL(t.abstract,'')
END



Poor planning on your part does not constitute an emergency on my part.






Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-02-09 : 22:42:17
[code]
case when len(t.abstract) > 200
then
case when charindex(' ', reverse(left(t.abstract, 200))) <> 0
then left(t.abstract, 200 - charindex(' ', reverse(left(t.abstract, 200)))) + '...'
else left(t.abstract, 200) + '...'
end
else
t.abstract
end
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -