如何在sqlite中用填充连接字符串

| 我在sqlite表中有三列:
    Column1    Column2    Column3
    A          1          1
    A          1          2
    A          12         2
    C          13         2
    B          11         2
我需要选择
Column1-Column2-Column3
(例如
A-01-0001
)。我想用ѭ3填充每一列 我是SQLite的初学者,我们将提供任何帮助     
已邀请:
  
||
运算符是\“ concatenate \”-将两个字符串连在一起   其操作数。 来自http://www.sqlite.org/lang_expr.html 对于填充,我使用的看似骗子的方法是从目标字符串开始,例如\'0000 \',连接\'0000423 \',然后为\'0423 \ substr(result,-4,4) '。 更新:看起来在SQLite中没有\“ lpad \”或\“ rpad \”的本机实现,但是您可以在此处遵循(基本上是我的建议):http://verysimple.com/2010/01/12 / sqlite-lpad-rpad-function /
-- the statement below is almost the same as
-- select lpad(mycolumn,\'0\',10) from mytable

select substr(\'0000000000\' || mycolumn, -10, 10) from mytable

-- the statement below is almost the same as
-- select rpad(mycolumn,\'0\',10) from mytable

select substr(mycolumn || \'0000000000\', 1, 10) from mytable
外观如下:
SELECT col1 || \'-\' || substr(\'00\'||col2, -2, 2) || \'-\' || substr(\'0000\'||col3, -4, 4)
它产生
\"A-01-0001\"
\"A-01-0002\"
\"A-12-0002\"
\"C-13-0002\"
\"B-11-0002\"
    
SQLite具有一个
printf
函数,该函数完全可以做到:
SELECT printf(\'%s-%.2d-%.4d\', col1, col2, col3) FROM mytable
    
@tofutim答案仅需一行...如果您想要自定义字段名称作为串联行...
SELECT 
  (
    col1 || \'-\' || SUBSTR(\'00\' || col2, -2, 2) | \'-\' || SUBSTR(\'0000\' || col3, -4, 4)
  ) AS my_column 
FROM
  mytable;
在SQLite 3.8.8.3上测试,谢谢!     

要回复问题请先登录注册