0

我有一张带有地名的表格。有时他们前面还有额外的字母。例如,如果城市名称是阿斯塔纳,那么它就是s.Astana。我只需要获取阿斯塔纳地点的名称。我想使用substr,但我找不到第一个大写字母的索引。哦,使用西里尔字符也是一个问题。我想举几个例子:

  1. Акмолинская область -> Акмолинская
  2. Кокшетау Г.А. -> Кокшетау
  3. г.Кокшетау -> Кокшетау
  4. Красноярский с.о. -> Красноярский
  5. с.Красный Яр -> Красный Яр

我尝试过的:创建了一个字段 short_nameru 并以这种方式更新了它

UPDATE center_kato ck SET ck.short_nameru = case when 
  LENGTH(SUBSTRING_INDEX(ck.nameru , ' ', -1)) > LENGTH(SUBSTRING_INDEX(ck.nameru , ' ',1))
  then SUBSTRING_INDEX(ck.nameru , ' ', -1)
  else SUBSTRING_INDEX(ck.nameru , ' ',1) 
  END

之后我再次更新了它

UPDATE center_kato ck SET ck.short_nameru = case 
  when LENGTH(SUBSTRING_INDEX(ck.short_nameru , '.', -1)) > LENGTH(SUBSTRING_INDEX(ck.short_nameru , '.',1))
  then SUBSTRING_INDEX(ck.short_nameru , '.', -1)
  WHEN LENGTH(SUBSTRING_INDEX(ck.short_nameru , '.', -1)) < LENGTH(SUBSTRING_INDEX(ck.short_nameru , '.',1)) 
  then SUBSTRING_INDEX(ck.short_nameru , '.',1)
  ELSE ck.short_nameru 
  END

但我失去了一些需要的价值。"зимовка Туяк"变成了"зимовка",但我需要"Туяк"

4

3 回答 3

2

您可以使用SUBSTRING_INDEX函数:

SELECT
  SUBSTRING_INDEX(city, '.', -1) as Place
FROM
  yourtable

负值 (-1) 将从字符串的尾部执行反向搜索并返回尾部(最右边)部分。

编辑

鉴于您上面的示例数据,我认为这个查询应该返回您需要的内容:

SELECT
  yourtable.id,
  SUBSTRING_INDEX(SUBSTRING(city, first_upper), ' ', 1) city_name
FROM
  yourtable INNER JOIN (
    SELECT
      id,
      MIN(CASE WHEN (MID(yourtable.city, numbers.n, 1) != '\.') AND
                    (UPPER(MID(yourtable.city, numbers.n, 1))=BINARY
                    MID(yourtable.city, numbers.n, 1))
               THEN numbers.n END) first_upper
    FROM
      numbers, yourtable
    GROUP BY
      id) f
  ON yourtable.id = f.id;

请看看这个小提琴

您需要一个包含从 1 到字符串最大长度的数字的数字表来模拟 for 循环。在我的子查询中,我正在计算每个城市的字符串中不包含 a 的最小位置,并且该位置中包含.的字符等于大写字符(我们需要在这里进行二进制比较)。如果条件为真,则表示该字符已经是大写的(或者表示它是一个特殊字符,例如; ! ?但我们需要排除它们)。

然后我从计算的位置中提取子字符串,直到第一个空格字符。

于 2013-07-25T10:48:16.547 回答
1

这是 SQLFiddel 演示
下面是方法:

SELECT substring_index(col1, 
                       '.', 
                       -1) as Place
FROM temp
WHERE col1 LIKE '%\.%'
于 2013-07-25T10:54:55.080 回答
0

我想不出一种在字符串中找到模式的方法。这是一种蛮力方法:

select (case when ascii(substr(CityName, 1, 1)) between ascii('A') and ascii('Z')
             then CityName
             when ascii(substr(CityName, 2, 1)) between ascii('A') and ascii('Z')
             then substr(CityName, 2)
             when ascii(substr(CityName, 3, 1)) between ascii('A' and ascii('Z')
             then substr(CityName, 3)
             when ascii(substr(CityName, 4, 1)) between ascii('A') and ascii('Z')
             then substr(CityName, 4)
             when ascii(substr(CityName, 5, 1)) between ascii('A') and ascii('Z')
             then substr(CityName, 5)
             when ascii(substr(CityName, 6, 1)) between ascii('A') and ascii('Z')
             then substr(CityName, 6)
             when ascii(substr(CityName, 7, 1)) between ascii('A') and ascii('Z')
             then substr(CityName, 7)
             when ascii(substr(CityName, 8, 1)) between ascii('A') and ascii('Z')
             then substr(CityName, 8)
             when ascii(substr(CityName, 9, 1)) between ascii('A') and ascii('Z')
             then substr(CityName, 9)
             when ascii(substr(CityName, 10, 1)) between ascii('A') and ascii('Z')
             then substr(CityName, 10)
             else CityName
        end)

如果您知道前面的字符始终是 a '.',那么还有其他方法。

编辑:

以下似乎在西里尔语中起作用:

select    
    (case when substr(nameru, 1, 1) between BINARY 'А' AND BINARY 'Я' then nameru
    when substr(nameru, 2, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 2)
    when substr(nameru, 3, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 3)
    when substr(nameru, 4, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 4)
    when substr(nameru, 5, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 5)
    when substr(nameru, 6, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 6)
    when substr(nameru, 7, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 7)
    when substr(nameru, 8, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 8)
    when substr(nameru, 9, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 9)
    when substr(nameru, 10, 1) between BINARY 'А' AND BINARY 'Я' then substr(nameru, 10)
         else nameru
        end)
FROM mytable;

(出于某种原因,编辑拒绝了 yernanun 的编辑,所以我将其包括在内。)

于 2013-07-25T11:05:03.357 回答