String processing (Fuzzy unordered search)

Source: Internet
Author: User

-----------------------------------

-- Author: htl258 (Tony)

-- Title: string processing (Fuzzy unordered search)

-- Date: 21:26:00

Http://blog.csdn.net/htl258

-- Keep this information for reference.

-----------------------------------

If not object_id ('ta ') is null

Drop table Ta

Go

Create Table TA (ID varchar (3), title varchar (39 ))

Insert into Ta

Select '001', 'Go deep into sqlserver2005 Development Management and Application instance' Union all

Select '002 ', 'sqlserver2005 Performance Tuning' Union all

Select '003 ',' SQL full Manual' Union all

Select '004 ', 'sqlserver2005 technology insider storage engine' Union all

Select '005 ',' SQL book 'Union all

Select '006 ', 'sqlserver2000 advanced management and developing' Union all

Select '007 ', 'sqlserver2005 database development details' Union all

Select '008 ', 'sqlserver2000 stored procedure and XML programming' Union all

Select '009', 'sqlserver2005 System Management records' Union all

Select '010 ',' SQL Technical Manual'

Go

 

-- Create a UDF

 

If not object_id ('fn _ str') is null

Drop function fn_str

Go

Create Function fn_str (

@ STR varchar (100), -- input string
@ Flag varchar (10) -- delimiter
)

Returns @ t table (A varchar (10 ))

As

Begin

Set @ STR = rtrim (ltrim (@ Str ))

While Len (@ Str)> 0

Begin

Insert @ t select left (@ STR, charindex (@ flag, @ STR + @ flag)-1)

Set @ STR = ltrim (stuff (ltrim (@ Str), 1,

Charindex (@ flag, ltrim (@ Str) + @ flag ),''))

End

Return

End

Go

 

 

-- 1. Full match of all keywords

Declare @ s varchar (100)

Set @ s = 'SQL 2000'

Select * From ta t

Where not exists (

Select 1

From fn_str (@ s ,'')

Where charindex (A, T. Title) = 0)

/*

Id title

-------------------------------------------

006 sqlserver2000 advanced management and development

 

(One row is affected)

*/

 

 

-- 2. Partial keyword matching

Set @ s = 'development 100'

Select * From ta t

Where exists (

Select 1

From fn_str (@ s ,'')

Where charindex (A, T. Title)> 0)

/*

Id title

-------------------------------------------

001 in-depth introduction to sqlserver2005 Development Management and Application Instances

006 sqlserver2000 advanced management and development

007 sqlserver2005 database development details

008 sqlserver2000 stored procedure and XML Programming

 

(Four rows affected)

*/

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/htl258/archive/2009/09/07/4525799.aspx

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.