从字符串SQL中提取位置名称

时间:2013-07-17 05:52:09

标签: sql

我想知道有没有办法从给定的字符串中提取位置名称? 例如 string ='周末拉斯维加斯派对'或'巴黎时尚更新' 从我想要的两个字符串:'拉斯维加斯'或'巴黎'

由于

2 个答案:

答案 0 :(得分:2)

创建一个包含预定义位置的表:

CREATE TABLE locations 
  ( 
     [name] VARCHAR(50) 
  ); 

INSERT INTO locations 
            ([name]) 
VALUES      ('Paris'), 
            ('Las Vegas'), 
            ('London'); 

CREATE TABLE [test] 
  ( 
     [input] VARCHAR(max) 
  ) 

INSERT INTO [test] 
            (input) 
VALUES      ('Las Vegas parties at weekends'), 
            ('Paris fashion updates'), 
            ('Paris and London fashion weeks') 

然后将其与输入字符串表连接:

SELECT
    t.Input,
    l.Name
FROM 
    Locations AS l
INNER JOIN test AS t ON t.Input LIKE '%' + l.Name + '%'
ORDER BY
    t.Input

这是SQL Fiddle

答案 1 :(得分:0)

假设您有一个包含位置列表的表格(例如locations (name varchar(100))),您可以执行以下操作:

select name from locations where @input like '%'+name+'%'

@input是您的输入字符串('巴黎时尚更新'等)。

Here is a working example

顺便说一句,如果您使用的是SQL Server,这里有一个技巧可以在没有while循环的单个字符串中获取输出:

select @output = @output + ' ' + name 
from locations where @input like '%'+name+'%'

请参阅updated fiddle