Note that there are some explanatory texts on larger screens.

plurals
  1. POMatching multiple variations of input to one sql row
    primarykey
    data
    text
    <p>I would like to know after <em>much</em> searching how I would match different variations of input to one sql row using standard TSQL. Here is the scenario:</p> <p>I have in my sql row the following text: <code>I love</code></p> <p>I then have the following 3 inputs all of which should return a match to this row:</p> <blockquote> <p>"<strong>I</strong> want to tell you we all <strong>love</strong> StackOverflow"</p> <p>"<strong>I</strong>'m totally in <strong>love</strong> with StackOverflow"</p> <p>"<strong>I</strong> really <strong>love</strong> StackOverflow"</p> </blockquote> <p>As you can see I have bolded the reason for the match to try and make it clearer to you why they match. The <code>I</code> in <code>I'm</code> is deliberately matched too so it would be good if we could include that in matches.</p> <p>I thought about splitting the input string which I done using the following TSQL:</p> <pre><code>-- Create a space delimited string for testing declare @str varchar(max) select @str = 'I want to tell you we all love StackOverflow' -- XML tag the string by replacing spaces with &lt;/x&gt;&lt;x&gt; tags declare @xml xml select @xml = cast('&lt;x&gt;&lt;![CDATA['+ replace(@str,' ',']]&gt;&lt;/x&gt;&lt;x&gt;&lt;![CDATA[') + ']]&gt;&lt;/x&gt;' as xml) -- Finally select values from nodes &lt;x&gt; and trim at the same time select ltrim(rtrim(mynode.value('.[1]', 'nvarchar(12)'))) as Code from (select @xml doc) xx cross apply doc.nodes('/x') (mynode) </code></pre> <p>This gets me all the words as separate rows but then I could not work out how to do the query for matching these.</p> <p>Therefore any help from this point or any alternate ways of matching as required would be more than greatly appreciated!</p> <p><strong>UPDATE:</strong></p> <p>@freefaller pointed me to the RegEx route and creating a function I have been able to get a bit further forward, therefore +1 @freefaller, however I now need to know how I can get it to look at all my table rows rather than the hard-coded input of 'I love' I now have the following select statements:</p> <pre><code>SELECT * FROM dbo.FindWordsInContext('i love','I want to tell you we all love StackOverflow',30) SELECT * FROM dbo.FindWordsInContext('i love','I''m totally in love with StackOverflow',30) SELECT * FROM dbo.FindWordsInContext('i love','I really love StackOverflow',30) </code></pre> <p>The above returns me the number of times matched and the context of the string matched, therefore the first select above returns:</p> <pre><code>Hits Context 1 ...I want to tell you we all love StackOv... </code></pre> <p>So based on the fact we now have the above can anyone tell me how to make this function look at all of the rows for matches and then return the row/rows that have a match?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload