Sqlite:遍历字符串字段的子字符串

时间:2012-07-31 10:06:34

标签: android sql sqlite for-loop substring

我有一个字段,其中包含一串数字

 "2002 2005 2001 2006 2008 2344"

我们假设空格是分裂,否则最好不要这样,它们每个都是4个字符。

我可以选择子字符串:

SELECT substr(years,1,4)  FROM TABLE 

但不知道如何检查每一个。我试图找到包含最接近0的数字的行。

我可以ORDER BYLIMIT 1

2 个答案:

答案 0 :(得分:2)

这看起来很丑,但它在一个SQLlite语句中选择了你需要的所有东西。如果假设日期在当前时间间隔1900..2100,您也可以进行一些优化。在这种情况下,您可以先切2个选择B,C(B:1联合2)(C:9联合0联合1)

 select years2,years from 
    (
    select i1||i2||i3||i4  as years2 from (select '0' as i1 
                   union all 
                   select '1' as i1
                   union all 
                   select '2' as i1
                   union all 
                   select '3' as i1
                   union all 
                   select '4' as i1 
                   union all 
                   select '5' as i1 
                   union all 
                   select '6' as i1
                   union all 
                   select '7' as i1
                   union all 
                   select '8' as i1
                   union all 
                   select '9' as i1) B, 
    (select '0' as i2 
                   union all 
                   select '1' as i2
                   union all 
                   select '2' as i2
                   union all 
                   select '3' as i2
                   union all 
                   select '4' as i2 
                   union all 
                   select '5' as i2 
                   union all 
                   select '6' as i2
                   union all 
                   select '7' as i2
                   union all 
                   select '8' as i2
                   union all 
                   select '9' as i2) C,
    (select '0' as i3 
                   union all 
                   select '1' as i3
                   union all 
                   select '2' as i3
                   union all 
                   select '3' as i3
                   union all 
                   select '4' as i3 
                   union all 
                   select '5' as i3 
                   union all 
                   select '6' as i3
                   union all 
                   select '7' as i3
                   union all 
                   select '8' as i3
                   union all 
                   select '9' as i3) D,
                  (select '0' as i4 
                   union all 
                   select '1' as i4
                   union all 
                   select '2' as i4
                   union all 
                   select '3' as i4 
                   union all 
                   select '4' as i4 
                   union all 
                   select '5' as i4 
                   union all 
                   select '6' as i4
                   union all 
                   select '7' as i4
                   union all 
                   select '8' as i4
                   union all 
                   select '9' as i4) E
    ) YearsAll

   left join YearsTable on (YearsTable.years like '%'||YearsAll.years2||'%')

    where YearsTable.years is not null

    order by years2 limit 1

答案 1 :(得分:1)

您可以使用公用表格表达式来分割年份字符串中的年份,然后获得最接近0的年份。

WITH split_years(year, years) AS (
    values(0, '2001 2002 2003 2004 2005 2006'||' ')
    UNION ALL SELECT
    cast(substr(years, 0, instr(years, ' ')) as int),
    substr(years, instr(years, ' ')+1)
    FROM split WHERE length(years)
) SELECT year FROM split_years WHERE year>0 ORDER BY year LIMIT 1;

输出是:

2001