tsql匹配缩写

时间:2012-03-29 18:17:42

标签: sql sql-server tsql

我需要匹配abreviations,其中一个点表示开头的所有内容,例如:

Rows:
  blue love
  abo love
  comeb love
  blue lauer
  blue alo

(a)

Input:
  b. love

Expected output:
  blue love

(b)

Input:
 b. l.

Expected output:
  blue love
  blue lauer

任何提示?

2 个答案:

答案 0 :(得分:5)

你可以转换。到%,以便您可以执行LIKE

SELECT @yourfilter = REPLACE(@yourfilter, '.', '%');

然后使用它:

SELECT * FROM TABLE WHERE COLUMN LIKE @yourfilter

这相当于:

SELECT * FROM TABLE WHERE COLUMN LIKE 'b% love'

OR

SELECT * FROM TABLE WHERE COLUMN LIKE 'b% l%'

Here is a working SQL Fiddle example

答案 1 :(得分:1)

您可以执行以下操作:

CREATE PROCEDURE abrieviate
(
@delim          char,
@word           varchar(20)
)

AS

SELECT TOP 1 * 
FROM Table
WHERE CHARINDEX(WORD_FIELD, @delim) > 0
AND WORD_FIELD LIKE '%' + @word + '%'

GO

EXEC abrieviate 'b', "love"