Aks*_*ara 205 sqlite string string-concatenation leading-zero
我在sqlite表中有三列:
Column1 Column2 Column3
A 1 1
A 1 2
A 12 2
C 13 2
B 11 2
Run Code Online (Sandbox Code Playgroud)
我需要选择Column1-Column2-Column3
(例如A-01-0001
).我想用一个填充每列-
我是一个关于SQLite的初学者,任何帮助将不胜感激
tof*_*tim 368
该
||
运营商的"串联" -它的操作数的两个字符串连接在一起.
来自http://www.sqlite.org/lang_expr.html
对于填充,我使用的看似欺骗的方式是从你的目标字符串开始,比如'0000',连接'0000423',然后是'0423'的substr(结果,-4,4).
更新:看起来在SQLite中没有"lpad"或"rpad"的本机实现,但你可以跟随(基本上我提出的)这里:http://verysimple.com/2010/01/12/sqlite-lpad -rpad功能/
-- 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
Run Code Online (Sandbox Code Playgroud)
以下是它的外观:
SELECT col1 || '-' || substr('00'||col2, -2, 2) || '-' || substr('0000'||col3, -4, 4)
Run Code Online (Sandbox Code Playgroud)
它产生了
"A-01-0001"
"A-01-0002"
"A-12-0002"
"C-13-0002"
"B-11-0002"
Run Code Online (Sandbox Code Playgroud)
ybu*_*ill 36
SQLite有一个printf
功能就是这样:
SELECT printf('%s-%.2d-%.4d', col1, col2, col3) FROM mytable
Run Code Online (Sandbox Code Playgroud)
Mad*_*ota 16
只需再为@tofutim回答一行......如果你想要连续行的自定义字段名...
SELECT
(
col1 || '-' || SUBSTR('00' || col2, -2, 2) | '-' || SUBSTR('0000' || col3, -4, 4)
) AS my_column
FROM
mytable;
Run Code Online (Sandbox Code Playgroud)
在SQLite 3.8.8.3上测试,谢谢!
归档时间: |
|
查看次数: |
225606 次 |
最近记录: |