Search code examples
sql-serverfunctiont-sqlsql-server-2014

Need to match exact string within a varchar column in SQL Server


I have a two-part problem in which I am pulling all #hashtags from a column to display which #hashtags were "trending" for a particular time frame to display on an SSRS report. And then I have a sub-report in which the user can click the #hashtag and be taken to all records from the table in which the #hashtag appeared.

To pull the #hashtags from the table, this thread showed me how I could accomplish this feat using two functions and a cross apply.

Now I am trying to find an exact match of a text string in a varchar(max) column from a table in SQL Server. My current query uses a LIKE statement:

ticketDescription LIKE  '%' + @paramHashtag + '%'

This presents a problem because I do have similar #hashtags such as #update and #updateinfo and the LIKE statement returns #updateinfo hashtags when I am searching for #update hashtags.

I tried to use WHERE CONTAINS (ticketDescription, @paramHashtag) but we do not have full-text enabled and I am not sure that is a possiblility.

If full-text search is not an option, is it possible to look for exact matches on the parameter that will be passed to the query?

Below is sample data from the ticket descriptions:

CREATE TABLE [dbo].[Trending](
[TicketDescription] [varchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Values inside the TicketDescription column:

#Update  Corrected last name and driver's license number.
#Update  Update customer's last name.
#Update  Update last name, address
#update - Profile updated. Corrected last name.
#updateinfo
#updateinfo
#updateinfo
#updateinfo
#Update  Update residency status
#update #SSNdiscrepancy   John Doe called in claiming this was their SSN, please advise.
#update  Updated residency status
#Update  Changed residency from in-state to out-of-state
#Update  Updated Customer's last name
#updateinfo
#updateinfo
#Update - updated customer's last name

Solution

  • Here's an example of how you could write a query that should deliver the results you expect (by checking for a match on your hashtag and then a character that isn't a letter or a number - e.g. a space, comma, etc after the hashtag):

    DECLARE @T TABLE (TicketDescription VARCHAR(MAX));
    INSERT @T VALUES 
    ('junk text #Update  Corrected last name and driver''s license number.'),
    ('more junk text#Update  Update customer''s last name.'),
    ('asdfadsf #Update  Update last name, address'),
    ('#update - Profile updated. Corrected last name.'),
    ('#updateinfo'),
    ('#updateinfo'),
    ('#updateinfo'),
    ('#updateinfo'),
    ('#Update  Update residency status'),
    ('#update #SSNdiscrepancy   John Doe called in claiming this was their SSN, please advise.'),
    ('#update  Updated residency status'),
    ('#Update  Changed residency from in-state to out-of-state'),
    ('asdfasdfadf#Update  Updated Customer''s last name'),
    (' asdfadsf #updateinfo'),
    ('asdf #updateinfo'),
    ('#Update - updated customer''s last name'),
    ('#Update'),
    ('#Update,example with a comma after hashtag');
    
    DECLARE @param VARCHAR(255) = '#update';
    
    SELECT TicketDescription
    FROM @T
    WHERE TicketDescription LIKE '%' + @param + '[^A-Z0-9]%' -- Hashtag + something that isn't a letter or number.
    OR RIGHT(TicketDescription, LEN(@param)) = @param; -- Or it matches the very last part of the given TicketDescription.