Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:51:54+00:00 2026-06-01T10:51:54+00:00

Given a specific word pattern (say, balloon), I would like to find the number

  • 0

Given a specific word pattern (say, “balloon”), I would like to find the number of n words before and after, group by them, with a count that exist in the title of my table

For, example if the data set was:

  • red balloon sky
  • yellow balloon sky road
  • blue balloon chair

I’d like the results to be something like:

- red balloon | 1
- yellow balloon | 1
- blue balloon | 1
- balloon sky | 2
- balloon chair | 1

I figured the best way to accomplish this would be with regex in my sproc. So, I added the great regex functions listed here, and the FindWordsInContext function.

To start with:

WITH Words_CTE (Title)
AS
-- Define the CTE query.
(
    SELECT Title
    FROM ItemData
    WHERE Title LIKE '%balloon%'
)
-- Define the outer query referencing the CTE name.
SELECT Title
FROM Words_CTE

So I figured I would start with that and work the FindWordsInContext function into the mix, then do a grouping on the words/before a given word.

— UPDATE —

Thanks to Adrian Iftode below… but the code doesn’t exactly do what I’m looking for.

declare @table table(Sentence varchar(250))

insert into @table(sentence)
    values ('I have another red balloon in the car.'),
            ('Here is a new balloon for you.'),
            ('A red balloon is in the other room.'),
            ('Is there another balloon for me?')


select TOP(5) SentencePart, NumberOfWords
from @table
cross apply dbo.fnGetPartsFromSentence(Sentence, 'balloon') f
order by
  NumberOfWords DESC,
  case when f.Side = 'R' then 0
  else 1 end

Outputs:

balloon is in the other room.       5
I have another red balloon          4
Here is a new balloon               4
Is there another balloon            3
balloon in the car.                 3

I would like to be able to set the range on either side of “balloon”. In this case, let’s say one word, the output should be:

red balloon      2
new balloon      1
another balloon  1
balloon in       1
balloon for      2
balloon is       1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:51:55+00:00Added an answer on June 1, 2026 at 10:51 am

    Is a bit a lot of code, I’ll try to explain

    First I used a split function, is going to split a varchar by a given varchar

    CREATE FUNCTION [dbo].[fnSplitString](@str NVARCHAR(MAX),@sep NVARCHAR(MAX))
    RETURNS TABLE
    AS
    RETURN
        WITH a AS(
            SELECT CAST(0 AS BIGINT) AS idx1,
                   CHARINDEX(@sep,@str) idx2, 
                   1 as [Level]
            UNION ALL
            SELECT idx2 + coalesce(nullif(LEN(@sep),0),1),
                   CHARINDEX(@sep,@str, idx2 + 1), 
                   [Level] + 1 as [Level]
            FROM a
            WHERE idx2 > 0
        )
        SELECT SUBSTRING(@str,idx1,COALESCE(NULLIF(idx2,0),LEN(@str)+1)-idx1) AS Value, 
               [Level], 
               case when idx1 = 0 then 'R' when idx2 != 0 then 'LR' else 'L' end as Side
        FROM a  
    

    Given the varchar ‘red balloon sky’ and when the split is the space character it will output :

    select *
    from dbo.fnSplitString('red balloon sky', ' ')
    
    Value   Level   Side
    red      1       R
    balloon  2       LR
    sky      3       L
    

    The Side part means: if R then the space is on the right side of the word, if L then the space is on the left side of the word and if LR then the word is surrounded by spaces.

    When the split is ‘balloon’

    select *
    from dbo.fnSplitString('red balloon sky', 'balloon')
    
    red     1   R
     sky    2   L
    

    So the balloon appears on the right side of red and appears on the left side of sky

    Having this helpful function I created another function which will output the required format for a single sentence (varchar)

    create FUNCTION [dbo].[fnGetPartsFromSentence](@sentence NVARCHAR(MAX),@word NVARCHAR(MAX))
    RETURNS TABLE
    AS
    RETURN
    
    
    with RawData as
    (select rtrim(ltrim(f.Value)) as LR, 
           (select COUNT (*) from dbo.fnSplitString(rtrim(ltrim(f.Value)), ' ')) as NumberOfWords,
           f.Side,
           0 as SideLevel
    from dbo.fnSplitString(@sentence, @word) as f
    where f.Side = 'R' or f.Side = 'L'
    union all
    (
        select rtrim(ltrim(f.Value)) as LR, 
           (select COUNT (*) from dbo.fnSplitString(rtrim(ltrim(f.Value)), ' ')) as NumberOfWords,
           f.Side,
           sl.no as SideLevel
        from dbo.fnSplitString(@sentence, @word) as f
        join (select 1 as no union all select 2) sl on 1 = 1
        where f.Side = 'LR'
    )
    )
    select (case when Side = 'R' then LR + ' ' + @word 
                 when Side = 'L' then @word + ' ' + LR
                 when Side = 'LR' then  
                        (
                            case when SideLevel  = 1 then @word + ' ' + LR
                            when SideLevel  = 2 then LR + ' ' + @word 
                            end
                        )
                end) as SentencePart,
            (case when Side = 'R' or Side = 'L' then Side
                  else           
                       (    case when SideLevel  = 1 then 'L'
                            when SideLevel  = 2 then 'R'
                            end
                        )
                end) as Side,
            NumberOfWords           
    from RawData
    

    This function uses the previous one. First it splits the sentence by word and counts the words in the splits by doing another split by space. When a word appears on both sides of the split, it repeats the split (that join with 1, 2 values).

    This function will also output split concatenated with the word, depending on which side it is: left, right or both. It will also output the Side, this time is Left or Right.

    select *
    from [dbo].[fnGetPartsFromSentence]('yellow balloon sky road','balloon')
    
    SentencePart        Side    NumberOfWords
    
    yellow balloon      R           1
    balloon sky road    L           2
    

    Now using this function I can cross apply it with a table

    declare @table table(Sentence varchar(250))
    
    insert into @table(sentence)
        values ('red balloon sky'),
                ('yellow balloon sky road'),
                ('blue balloon chair')
    
    
    select SentencePart, NumberOfWords
    from @table
    cross apply dbo.fnGetPartsFromSentence(Sentence, 'balloon') f
    order by
      case when f.Side = 'R' then 0
      else 1 end
    

    The output is

    red balloon           1
    yellow balloon        1
    blue balloon          1
    balloon chair         1
    balloon sky road      2
    balloon sky           1
    

    Works and when there are multiple occurrences

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regex that gives me the word before and after a specific
Given a specific DateTime value, how do I display relative time, like: 2 hours
I have an array of ints like this: [32,128,1024,2048,4096] Given a specific value, I
When displaying all articles of a specific month (given through the urls like this
...meaning pattern in its general English usage, not specific to OO design patterns. Given
How to find all files with a specific key word in a directory with
I'm trying to understang how to use icu::BreakIterator to find specific words. For example
I need to draw a circle onto a bitmap in a specific colour given
Given that the tasks in a specific sprint will not divide perfectly into the
Given that two branches have diverged and a specific commit from one branch (and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.