Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transact-SQL: How do I tokenize a string?

Tags:

sql

tsql

I want to be able to tokenize an input string from a text box to do my query. Example: user enters "abc xyz 123" in the text box. I want to do this:

SELECT * FROM database WHERE Name contains "abc" AND "xyz" AND "123"
-- as opposed to containing "abc xyz 123"
-- please ignore my sql syntax, I am an absolute beginner

Thanks.

like image 298
user776676 Avatar asked Jun 02 '11 02:06

user776676


People also ask

How do you Tokenize in SQL?

use SQL::Tokenizer qw(tokenize_sql) ; my @tokens = tokenize_sql( $query ); my $tokens = tokenize_sql( $query ); $tokens = tokenize_sql( $query , $remove_white_tokens );

What is Tokenizing a string?

Tokenization is the act of breaking up a sequence of strings into pieces such as words, keywords, phrases, symbols and other elements called tokens. Tokens can be individual words, phrases or even whole sentences. In the process of tokenization, some characters like punctuation marks are discarded.

How do you Tokenize a Std string?

The common solution to tokenize a string in C++ is using std::istringstream , which is a stream class to operate on strings. The following code extract tokens from the stream using the extraction operator and insert them into a container.

What function we should use when we want to tokenize a string?

To perform sentence tokenization, we can use the re. split() function. This will split the text into sentences by passing a pattern into it.


2 Answers

Using a string split function (for example, like this one), you could have something like this:

SELECT t.*
FROM atable t
  INNER JOIN dbo.Split(@UserInput, ' ') s ON t.Name LIKE '%' + s.Data + '%'
like image 55
Andriy M Avatar answered Nov 15 '22 18:11

Andriy M


One possible solution is that you take the string and split it into your tokens and the for each token insert it into a temp table and then join you temp table to your search table and in the join do a like '%' + tokenColumn + '%' to get all your rows that contain a value from your tokens

Here's an example of splitting the string: http://blogs.microsoft.co.il/blogs/itai/archive/2009/02/01/t-sql-split-function.aspx

like image 37
Avitus Avatar answered Nov 15 '22 16:11

Avitus