用前导零计算数字

thtygnil  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(181)

我需要使这个过程能够处理存储为varchar的数据,其中一些行存储为前导零

CREATE PROCEDURE getfreenum
             (IN num integer,
              IN maxval integer)
BEGIN
  SELECT CASE
       WHEN NOT EXISTS (SELECT *
                               FROM artikli t2
                               WHERE t2.sifra = num * power(10, maxval - floor(log10(num)) - 1)) THEN
         num * power(10, maxval - floor(log10(num)) - 1)
       ELSE
         min(t1.sifra) + 1
     END sifra
     FROM artikli t1
     WHERE floor(log10(num)) < maxval
           AND EXISTS (SELECT *
                              FROM artikli t2
                              WHERE sifra = num * power(10, maxval - floor(log10(num)) - 1))
           AND NOT EXISTS (SELECT *
                                  FROM artikli t2
                                  WHERE t2.sifra = t1.sifra + 1)
           AND t1.sifra >= num * power(10, maxval - floor(log10(num)) - 1) - 1
           AND t1.sifra < num * power(10, maxval - floor(log10(num)) - 1) + power(10, maxval - floor(log10(num)) - 1) - 1;
END;

这是db fiddle查询#1应返回00004

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题