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 518399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:56:00+00:00 2026-05-13T07:56:00+00:00

Is there a way to parse a Google search string to a table variable

  • 0

Is there a way to parse a Google search string to a table variable in T-SQL?

By Google search string I mean, including the plus sign (require), minus sign (exclude), and exact phrase (double quotes) operators.

For example the following search string:

one -two +three "four five" -"six seven" +"eight nine" "ten eleven twelve"

Would be parsed to a table variable that I could use to generate a T-SQL where clause:

OPERATOR    STRING
            one
-           two
+           three
            four five
-           six seven
+           eight nine
            ten eleven twelve

Thanks!

  • 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-05-13T07:56:00+00:00Added an answer on May 13, 2026 at 7:56 am

    Well parsers are written the same way in any language, using a state machine. Nothing like writting a short parser in the morning to get your sinapses lubricated:

    declare @s varchar(max);
    declare @t table (operator char(1) null, token varchar(max));
    
    set @s = 'one -two +three "four five" -"six seven" +"eight nine" "ten eleven twelve"';
    
    declare @state varchar(100);
    declare @operator char(1);
    declare @token varchar(max);
    declare @c char(1);
    declare @i int;
    
    set @state = 'start';
    set @i = -1;
    
    while (1=1)
    begin
      set @i = @i + 1;
      if (@i > len(@s))
        break;
      set @c = substring(@s, @i, 1);
      if (@state = 'start')
      begin
        if @c in ('-', '+')
        begin
          set @operator = @c;
          set @token = '';
          set @state = 'operator';
          continue;
        end
        else if @c = '"'
        begin
          set @operator = null;
          set @token = '';
          set @state = 'quote';
          continue;
        end
        else if (@c between 'a' and 'Z')
          or (@c between '0' and '9')
        begin
          set @operator = null;
          set @token = @c;
          set @state = 'token';
          continue;
        end
        else
          continue; -- ignore noise
      end
      else if @state = 'token'
      begin
        if (@c between 'a' and 'Z')
          or (@c between '0' and '9')
        begin
          set @token = @token + @c;
          continue;
        end
        else
        begin
          insert into @t (operator, token)
            values (@operator, @token); 
          set @state = 'start';
          continue;
        end
      end
      else if @state = 'quote'
      begin
        if (@c != '"')
        begin
          set @token = @token+@c;
          continue;
        end
        else
        begin
          insert into @t (operator, token)
            values (@operator, @token); 
          set @state = 'start';
          continue;
        end
      end
      else if @state = 'operator'
      begin
        if @c = '"'
        begin
          set @token = '';
          set @state = 'quote';
          continue;
        end
        else if (@c between 'a' and 'Z')
          or (@c between '0' and '9')
        begin
          set @token = @c;
          set @state = 'token';
          continue;
        end
        else 
        begin
          -- consider raising error here, invalid char after operator +/-
          set @state = 'start';
          continue;
        end
      end
      else
        raiserror ('Unexpected state %s', 16,2, @state);
    end
    if @state = 'token'
    begin
      insert into @t (operator, token)
            values (@operator, @token); 
    end
    else if @state != 'start'
    begin
      raiserror ('Incorrectly formatted string, must not end in state %s', 16, 1, @state);
    end
    
    select * from @t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You didn't specify a language or development environment, so I… May 14, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Ensure you are using UTF-8 encoding on the site. Even… May 14, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Under the EJB 3.1 standard, EJB methods can very easily… May 14, 2026 at 10:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.