Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在sqlite中连接带填充的字符串_String_Sqlite_String Concatenation_Leading Zero - Fatal编程技术网

String 如何在sqlite中连接带填充的字符串

String 如何在sqlite中连接带填充的字符串,string,sqlite,string-concatenation,leading-zero,String,Sqlite,String Concatenation,Leading Zero,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的初学者,如有任何帮助,将不胜感激

|
操作符是“concatenate”-它将 它的操作数

对于填充,我使用的看似作弊的方法是从目标字符串开始,比如“0000”,连接“0000423”,然后用substr(result,-4,4)表示“0423”

更新:在SQLite中似乎没有“lpad”或“rpad”的本机实现,但您可以在这里遵循(基本上是我建议的):

下面是它的外观:

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"
这正是:

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;

继续测试,谢谢

可能重复:@Andrew-通常,任何涉及NULL的标量操作都会产生NULL。使用
COALESCE(可为null的_字段“”)| | COALESCE(另一个可为null的_字段“”)
可以满足您的要求。查询错误:没有此类函数:printf无法执行语句从mytable limit 7中选择printf('%s.%s',id,url)。我的版本是3.8.2 2014-12-06。您使用的是什么版本?“3.8.3”,还有其他一些小的增强,例如添加了printf()SQL函数
SELECT 
  (
    col1 || '-' || SUBSTR('00' || col2, -2, 2) | '-' || SUBSTR('0000' || col3, -4, 4)
  ) AS my_column 
FROM
  mytable;