-2

我有一个国家及其缩写表,其中有一列名为code缩写,另一列名为name包含国家名称。

我需要遍历一个外国地址并查找每个单词,直到在表中找到匹配项(即国家/地区)并检索案例陈述的缩写。

这不会在所有记录上完成,只有某些大于我正在构建的文件中 60 个字符的字段的记录。

所以我需要做的是影响:

SELECT 
CASE WHEN address2 & foreign_address > 60
THEN split and iterate through '12345 MY SUPER LONG ADDRESS IN THE PHILIPPINES' and look up
each string until PHILIPPINES is matched in the country_codes table and 'PH' is returned
END

这是我能想到的处理这种情况的最好方法,而不是截断由于明显原因我不想做的地址。这也需要根据不同的地址和国家而动态变化。

在这一点上,我最大的挑战是分解字符串并查看每个字符串片段。

4

4 回答 4

0
SELECT Addresses.foreign_address, Countries.Code
FROM Addresses, Countries
WHERE LEN(foreign_address) > 60
AND foreign_address LIKE '%' Countries.Name '%'

[代码] 将包含匹配国家的缩写代码。

这是相关的 SQLFiddle(以及下面的完整代码):

CREATE TABLE Countries (Name varchar(128), Code varchar(2));

CREATE TABLE Addresses (foreign_address varchar(512));

INSERT INTO Countries(Name,Code) VALUES('PHILIPPINES', 'PH'); 

INSERT INTO Addresses(foreign_address)
VALUES('12345 MY SUPER LONG ADDRESS IN THE PHILIPPINES UNTIL PHILIPPINES IS MATCHED AND PH IS RETURNED');

SELECT Addresses.foreign_address, Countries.Code
FROM Addresses, Countries
WHERE LEN(foreign_address) > 60
AND foreign_address LIKE '%' + Countries.Name + '%'
于 2013-08-19T21:35:53.900 回答
-1

像这样的东西,可能是?

    select l.abbr, a.id 
    from 
     lookup_table l, address_table a
    where 
     charindex(l.country_name, a.address2 + a.foreign_address) > 0 
     and len( a.address2 + a.foreign_address) > 60

未测试。

针对 SQL Server 2005 字符串连接运算符进行了编辑。

于 2013-08-19T21:07:38.560 回答
-1

You can use LIKE instead of breaking up by word:

declare @address2 varchar(128)
declare @foreign_address varchar(128)

set @address2 = '12345 MY SUPER LONG ADDRESS IN THE PHILIPPINES'
set @foreign_address = '123456789012345678901234567890123456789012345678901234567890'

SELECT 
CASE 
    WHEN len(@address2 + @foreign_address) > 60 
    AND (' ' + @address2 + ' ' + @foreign_address + ' ') like '% PHILIPPINES %' 
THEN 'PH' 
ELSE NULL 
END 
于 2013-08-19T21:24:54.363 回答
-2

我不提倡在 SQL Server 中使用这种编程风格,但这就是我将如何做你要求做的事情。首先,将字符串标记为表变量中的单个单词。然后在表变量上打开一个光标并循环遍历单词,break如果我们从 dbo.countries 中找到结果则调用。请注意,循环在 SQL Server 中非常低效。UDF 表函数来自这里:How to split a string in T-SQL?

-- Create the UDF
CREATE FUNCTION Splitfn(@String varchar(8000), @Delimiter char(1))       
returns @temptable TABLE (items varchar(8000))       
as       
begin       
    declare @idx int       
    declare @slice varchar(8000)       

    select @idx = 1       
        if len(@String)<1 or @String is null  return       

    while @idx!= 0       
    begin       
        set @idx = charindex(@Delimiter,@String)       
        if @idx!=0       
            set @slice = left(@String,@idx - 1)       
        else       
            set @slice = @String       

        if(len(@slice)>0)  
            insert into @temptable(Items) values(@slice)       

        set @String = right(@String,len(@String) - @idx)       
        if len(@String) = 0 break       
    end   
return      

end

go
-- Create the dbo.countries table so we can test our code later
create table dbo.countries (code char(2), name varchar(100))
go
-- Insert one record in dbo.countries so we can test our code later
insert into dbo.countries (code, name)
select 'PH', 'PHILIPPINES'
go
-- for one @String input, this is what I would do
declare @String varchar(1000) = '12345 MY SUPER LONG ADDRESS IN THE PHILIPPINES'
declare @CountryCode char(2) = ''
declare @done bit = 0x0
declare @word varchar(1000)

declare @words table (word varchar(250) primary key)

-- Break apart your @String into a table of records, only returning the DISTINCT values.  
-- Join on the domain list so we can only process the ones that will return data in the CURSOR (eliminating excess looping)
insert into @words (word)
--
select  distinct items as word
from    dbo.Splitfn(@String, ' ') s
join    dbo.countries c 
on      lower(c.name) = lower(s.items)

declare word_cursor CURSOR for
select  word
from    @words w

open word_cursor

fetch next from word_cursor into @word 

while @@FETCH_STATUS = 0
begin
    select      @CountryCode = code
    from        dbo.countries
    where       name = @word

    if @@trancount > 0
    begin
        break
    end

    fetch next from word_cursor into @word 
end

-- clean up the cursor
close word_cursor
deallocate word_cursor

-- return the found CountryCode
select @CountryCode
于 2013-08-19T23:27:09.770 回答