我在一个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)。我想用每个列填充 –
我是一个初学者关于sqlite,任何帮助将不胜感激
The
||
operator is “concatenate” – it joins together the two strings of
its operands.
从http://www.sqlite.org/lang_expr.html
对于填充,我使用的似乎骗子的方法是开始与你的目标字符串,说’0000’,连接’0000423’,然后substr(结果,-4,4)为’0423’。
更新:看起来在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,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"