如何在mysql,西里尔字符中找到第一个大写字母的索引

时间:2013-07-25 10:46:29

标签: mysql

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

  1. Акмолинскаяобласть - > Акмолинская
  2. КокшетауГ.А。 - > Кокшетау
  3. г.Кокшетау - > Кокшетау
  4. Красноярскийс.о。 - > Красноярский
  5. с.КрасныйЯр - > КрасныйЯр
  6. 我尝试过: 创建了一个字段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
    

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

3 个答案:

答案 0 :(得分: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;

请查看this fiddle

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

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

答案 1 :(得分:1)

<强> Here is SQLFiddel Demo
以下是方法:

SELECT substring_index(col1, 
                       '.', 
                       -1) as Place
FROM temp
WHERE col1 LIKE '%\.%'

答案 2 :(得分: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)

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

编辑:

以下似乎适用于西里尔语:

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的编辑,所以我将其包括在内。)