加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在sqlite中连接字符串和padding

发布时间:2020-12-12 19:22:03 所属栏目:百科 来源:网络整理
导读:我在一个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
我在一个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"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读