Fuzzy matching string _mssql for generating queries in SQL
Source: Internet
Author: User
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ F_sql] and Xtype in (n ' FN ', n ' IF ', n ' TF '))
Drop function [dbo]. [F_sql]
Go
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (n ' [ordinal] ') and OBJECTPROPERTY (ID, n ' isusertable ') = 1)
drop table [ordinal tables]
Go
--For efficiency, you need an auxiliary table to match
Select Top 1000 id=identity (int,1,1) into ordinal tables
From syscolumns A,syscolumns b
ALTER TABLE sequence tables add constraint pk_id_ sequence primary key (ID)
Go
/*--generates a query's fuzzy matching string based on the specified string
The key word for a conditional connection is and,or
You can specify any parentheses
The generated conditional expression is like fuzzy matching
--Jiangjian 2004.08 (please keep this information for reference)--*/
/*--Call Example
--Invoke the example
Select A=dbo.f_sql (Web or HTML or Internet) and (programmer or Developer) ', ' content ')
Select B=dbo.f_sql (' Web or HTML or Internet ', ' content ')
Select C=dbo.f_sql (' Web and HTML ', ' content ')
Select D=dbo.f_sql (' Web ', ' content ')
--*/
--Sample function
Create Function F_sql (
@str Nvarchar (1000),--the string to retrieve
@fdname sysname--retrieving in that field
) returns Nvarchar (4000)
As
Begin
DECLARE @r Nvarchar (4000)
Set @r= '
Select @r=@r+case
When substring (@str, Id,charindex (", @str +", id)-id) in (' or ', ' and ')
Then ' +substring (@str, Id,charindex (', @str + ', id)-id) + '
When substring (@str, id,1) = ' ('
Then ' ([' + @fdname + '] like '% '
+substring (@str, Id+1,charindex (', @str + ', id)-id-1)
+'%'''
When substring (@str, charindex (', @str + ', id) -1,1) = ') '
Then ' [' + @fdname + '] like '% '
+substring (@str, Id,charindex (', @str + ', id)-id-1)
+'%'')'
Else ' [' + @fdname + '] like '% '
+substring (@str, Id,charindex (', @str + ', id)-id)
+'%'''
End
From order Tables
where Id<=len (@str)
and charindex (', ' + @str, id)-id=0
Return (@r)
End
Go
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.