The processing of T-SQL---Multi-valued fuzzy query

Source: Internet
Author: User
Tags string splitter

Processing of multi-valued fuzzy query

The so-called multi-valued fuzzy query, is the application passed over a number of parameters, the parameters are split, after splitting, the key value of the split results are fuzzy query processing

For exact matches, whether it's a single key value or multiple key values, it's easy to handle and a lot of alternatives.

For fuzzy queries, a single key value is also very easy, that is ... where name like '%parameter% ' (Don't tell me the full blur inefficient and so on, I'm not doing performance comparisons here)

However, for the fuzzy processing of multiple key values, it is not too straightforward, such as the Name field, input ' Three, four, five ', the requirements are three, four, 53 characters to do fuzzy matching

Multi-valued fuzzy query can be treated as follows, welcome to provide a better way, thank you

CREATE TABLE [DBO]. [Student] (    [s#] [varchar] ( null,    [sname] [varchar) (50)  null,    [ssex] [varchar] (2)  null) insert into student  values  (1, ' Zhang San ', 1) insert into student values  (2, ' John Doe ', 1) insert into student  VALUES  (3, ' Harry ', 1) insert into student values  (4, ' Zhao Liu ', 1) insert into  student values  (5, ' Sun Seven ', 1)--exact match, whether it is a single value or multi-value, is very good processing select * from student where  Sname in  (' Zhang San ', ' John Doe ')--good handling of single-valued ambiguity select * from student where sname  like  '% five '-multi-value fuzzy matching, there is no such a direct-so-called multi-valued fuzzy matching, is the outside pass in multiple values, these values are separated, each value is to do fuzzy query--sql to write to meet functional requirements, but also concise clear, At first, I wanted to write a function.,--later thought, can deal with it; With ctenameas (    --This is a string splitter function      Select id,concat ('% ', s, '% ')  as name from dbo.f_splitstrtotable (' Four, five, six ', ', ')) SELECT&NBsp;* from studentwhere exists (    select 1 from ctename  where sname like name)--Query Results s# sname ssex2  John Doe  13  Harry  14   Zhao Liu  1 (3 row (s)  affected)

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>


The processing of T-SQL---Multi-valued fuzzy query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.