我在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初学者每一列,任何帮助,将不胜感激
我在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(result,-4,4)为'0423'。
更新:貌似没有本地实现“LPAD”或SQLite的“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 has a printf
function这正是这么做的:
SELECT printf('%s-%.2d-%.4d', col1, col2, col3) FROM mytable
查询错误:无此功能:printf无法执行语句 从mytable限制中选择printf('%s。%s',id,url)7.我的版本是2014-12-06 3.8.2。你使用什么版本? – 2014-11-02 15:10:17
@BerryTsakala:3.8.6 – ybungalobill 2014-11-08 19:28:22
只需再行@tofutim答案...如果你想为级联排自定义字段名称...
SELECT
(
col1 || '-' || SUBSTR('00' || col2, -2, 2) | '-' || SUBSTR('0000' || col3, -4, 4)
) AS my_column
FROM
mytable;
在测试了SQLite 3.8.8.3,谢谢!
可能的重复:http://stackoverflow.com/q/3568779/2291 – 2012-10-31 20:48:13