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
 Simple Newb level query.

Author  Topic 

winterh
Posting Yak Master

127 Posts

Posted - 2009-10-13 : 06:00:01
Morning guys,

Do you know how I would get this to work (with a set amount of characters to be showed to the left and right of each searched phrase? (I would need it so If someone wants to search 'flu', it returns the word flu, then the 8 characters to the left and right)

declare @Phrase varchar(max)

set @Phrase= char(32)+'this is a sample of what I want to find'

select @Phrase=replace(@Phrase, ' '+8 Diagnosis +8' ',' ')
from Consultation

select @Phrase

GO

Thanks. H.


[/fail at query]

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-13 : 06:12:18
[code]select @Phrase, substring(@Phrase, charindex(@Word, @Phrase) - 8, len(@Word) + 16)[/code]


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

Go to Top of Page

winterh
Posting Yak Master

127 Posts

Posted - 2009-10-13 : 06:18:43
Cheers khtan. =)

[/fail at query]
Go to Top of Page

winterh
Posting Yak Master

127 Posts

Posted - 2009-10-13 : 06:20:02
What is @word? How do you declare it?

[/fail at query]
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-13 : 06:25:54
just declare it as a varchar variable

declare @word varchar(100)


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

Go to Top of Page

winterh
Posting Yak Master

127 Posts

Posted - 2009-10-13 : 06:34:49
Cheers khtan. =)

[/fail at query]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-13 : 07:10:24
declare @Phrase varchar(1000),@word varchar(1900)
set @Phrase= 'testing this is flu a sample of what I want to find'
set @word='flu'
select @Phrase as actual_word, @word as word_to_find,substring(@Phrase, charindex(@Word, @Phrase)-8,8) as left_eight,
substring(@Phrase, charindex(@Word, @Phrase)+len(@word),8) as right_eight


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

winterh
Posting Yak Master

127 Posts

Posted - 2009-10-13 : 07:26:40
Thats what I like to see, a fully working reply. Madhivanan, Once again- Your the man!
I would love to see inside your brain. :P

[/fail at query]
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-13 : 07:30:52
quote:
Originally posted by winterh

I would love to see inside your brain. :P

[/fail at query]



are you Sylar ?


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

Go to Top of Page

winterh
Posting Yak Master

127 Posts

Posted - 2009-10-13 : 07:49:19
maybe I am Sylar. OR Maybe I am Dancing Bojangles.

[/fail at query]
Go to Top of Page
   

- Advertisement -