0

我正在使用 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
 --------------------------------------------------------------------------

目前LocationIdCompany表中为空。如果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。

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

4

1 回答 1

4

尝试以下查询:

1.STEP1:制作一个函数,可以按任意字符分割字符串,并以表格形式返回输出。

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

这是做到这一点的一种方法。我们也可以使用全文搜索来做到这一点。

于 2013-07-06T07:27:04.967 回答