在列中查找特定单词

时间:2013-07-06 07:19:40

标签: sql-server sql-server-2008

我正在使用SQL Server 2008。

我的表是:

位置

------------------------
 Id  |  LocationName
------------------------
  1  |  Bodakdev
  2  |  Thaltej Road
  3  |  Andheri East
  4  |  Noida Sector 2

公司

 --------------------------------------------------------------------------
   CId  |  Address                                            | LocationId
 --------------------------------------------------------------------------
   11   |   301, GNFC Infotower, Bodakdev,                    |    NULL
   12   |   307/308,Arundeep Complex                          |    NULL
   13   |   7 Krishana Dyeing Compund, Nagardas rd., Andheri  |    NULL
   14   |   B-23 ,Ground Floor,Sector 2                       |    NULL
 --------------------------------------------------------------------------

目前LocationId表中的Company为空。如果Address包含任何位置名称,请更新LocationId

例如,CID-11的地址包含Bodakdev然后更新LocationId 1,第二个示例,CID的地址 - 13包含Andheri个字然后更新LocationId 3.

必需的输出:

  CId  |  Address                                            | LocationId
 --------------------------------------------------------------------------
   11   |   301, GNFC Infotower, Bodakdev,                    |    1
   12   |   307/308,Arundeep Complex                          |    NULL
   13   |   7 Krishana Dyeing Compund, Nagardas rd., Andheri  |    3
   14   |   B-23 ,Ground Floor,Sector 2                       |    4
 --------------------------------------------------------------------------

我尝试使用以下查询

SELECT   
   (LEN(Address) - LEN(REPLACE(Address, LocationName, '')) ) / LEN(LocationName) 

如果Address包含位置名称,则它将返回出现次数,否则返回0.

但它不会给出正确的输出。我怎样才能做到这一点?谢谢。任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:4)

尝试以下查询:

1.STEP1:创建一个可以按任意字符分割sting的函数,并以表格格式返回输出。

CREATE FUNCTION [dbo].[fnSplit](
    @sInputList VARCHAR(8000) -- List of delimited items
  , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
 BEGIN
 SELECT
  @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
  @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

 IF LEN(@sItem) > 0
  INSERT INTO @List SELECT @sItem
 END

IF LEN(@sInputList) > 0
 INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END

2.STEP2:使用以下查询来获得您的欲望输出。

    DECLARE @LOCATION AS TABLE (ID INT ,NAME VARCHAR(MAX))  
DECLARE @COMPANY AS TABLE (CID INT , ADDRESS VARCHAR(MAX) , LOCATIONID INT)     

INSERT INTO @LOCATION VALUES(1,'Bodakdev')
INSERT INTO @LOCATION VALUES(2,'Thaltej Road')
INSERT INTO @LOCATION VALUES(3,'Andheri East')
INSERT INTO @LOCATION VALUES(4,'Noida Sector 2')

INSERT INTO @COMPANY VALUES(11,'301, GNFC Infotower, Bodakdev,' , NULL)
INSERT INTO @COMPANY VALUES(12,'307/308,Arundeep Complex' , NULL)
INSERT INTO @COMPANY VALUES(11,'7 Krishana Dyeing Compund, Nagardas rd., Andheri' , NULL)
INSERT INTO @COMPANY VALUES(11,'B-23 ,Ground Floor,Sector 2' , NULL)

UPDATE @Company
SET
LOCATIONID = B.ID
FROM @COMPANY AS A , @LOCATION AS B 
WHERE 
1 = CASE WHEN 
            (
                SELECT COUNT(*)
                FROM FNSPLIT(B.NAME , ' ')
                WHERE A.ADDRESS LIKE '%' + ITEM +  '%'
            ) > 0 THEN 1 ELSE 0 END

这是一种方法。我们也可以使用全文搜索来完成。