Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String manupulation

Status
Not open for further replies.

Fatih235

MIS
May 30, 2002
19
0
0
US
Hi all;

I have a column called news. In every row of this column there is a news approximately 150 chars long. Each of these rows contains the word "exactly". I want to select 20 characters before "exactly", the word "exactly" and 20 characters after "exactly" . Any suggestions?
 
Try this:

select substring(news,charindex('exactly',news)-20,47) from table

Hope this helps
 
Select Substring(news, (Charindex('exactly', news, 1)) - 20, (Charindex('exactly', news, 1)) + len('exactly') + 20) From TableName
 
Just to suggest a scenario which may be possible. Word exactly may come within first 20 characters in news column. So just to make it a little more generic


select substring(news,charindex('exactly',news) -
case when charindex('exactly',news)> 20 then 20 else charindex('exactly',news) - 1 end
, case when charindex('exactly',news)> 20 then 20 else charindex('exactly',news) - 1 end + 27)
from table
where charindex('exactly',news) > 0

RT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top