SQLite 函数用前导零格式化数字?

发布于 2024-10-16 01:41:17 字数 342 浏览 6 评论 0原文

我有一个需要包含产品的 SQLite 数据库。这些产品具有固定格式的复合产品编号,由类别、组和产品编号 (cccccc.gg.ppp) 组成。每次插入新产品时,应使用类别和组编号来构建新产品编号,然后是该组中的最高产品编号加 1。

为此,我正在考虑将所有三个数字存储在不同的字段中,并使用视图动态组合产品编号。为此,我需要添加前导零,因为产品编号的格式是固定的。

但是,我似乎找不到一个内置的 SQLite 函数可以让我这样做...:-(

我不想编写自定义函数,因为我们可能必须与其他开发人员共享数据库,因为他们从中读取数据;我不知道他们将使用什么平台或语言来

完成此操作?还是我们必须找到不同的解决方案?

I’ve got an SQLite database that will need to contain products. Those products have a fixed-format composite product number, consisting of category, group and product number (cccccc.gg.ppp). Every time a new product is inserted, the new product number should be built using the category and group numbers, followed by the highest product number in that group incremented with one.

To that end, I was thinking of storing all three numbers in separate fields, and use a view to dynamically assemble the product number. For that to work, I would need to add leading zeroes, since the product number’s format is fixed.

However, I can’t seem to find a built-in SQLite function which would let me do this... :-(

I’d rather not write a custom function, since we might have to share the database with other developers, for them to read data from; and I have no idea what platform or language they’re going to use.

Can this be done? Or will we have to find a different solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海蓝天 2024-10-23 01:41:17

这可以通过使用字符串连接和 substr 函数来实现。长话短说,这就是您想要的:

select substr('0000000000'||'1234', -10, 10)

以下是解释。

首先,知道SQLite的字符串连接运算符是||

我们首先获取一个与我们想要返回的值一样长的字符串。例如,如果您想要返回一个始终为 10 个字符长的数字,并在其较短的情况下用 0 进行填充,则以十个 0 的字符串开始代码>s.为此,我们将连接您要返回的数字。在我们的示例中,即“1234”。到目前为止,我们的部分如下所示: '0000000000'||'1234'

然后,将整个内容传递到 substr 函数中。 根据文档,以下是 substr 函数的工作原理:

substr(X,Y,Z) 函数返回输入字符串 X 的以第 Y 个字符开头且长度为 Z 个字符的子字符串。 ...如果 Y 为负数,则通过从右侧而不是左侧计数来找到子字符串的第一个字符。

因此,如果您希望字符串长度为 10 个字符,请传递 -10 作为 Y 参数(从右侧开始计数,向后 10 个字符),并传递 10 作为 Z 参数(总共 10 个字符)。

This can be accomplished with a little bit of magic using string concatenation and the substr function. Long story short, here's what you want:

select substr('0000000000'||'1234', -10, 10)

An explanation follows.

First, know that SQLite's string concatenation operator is ||.

We'll start by taking a string that is as long as the value we want to return. For example, if you want to return a number that is always 10 characters long and have it be left padded with 0s if it is shorter, then start with a string of ten 0s. To that, we will concatenate the number you want to return. In our example, that is '1234'. So far, we have the part that looks like this: '0000000000'||'1234'

Then, pass that whole thing into the substr function. According to the documentation, here's how the substr function works:

The substr(X,Y,Z) function returns a substring of input string X that begins with the Y-th character and which is Z characters long. ... If Y is negative then the first character of the substring is found by counting from the right rather than the left.

So if you want the string to be 10 characters long, pass -10 as the Y parameter (to start counting from the right, 10 characters back) and pass 10 as the Z parameter (to take 10 characters total).

姐不稀罕 2024-10-23 01:41:17

从 3.8.3 (2014) 开始,您可以使用 printf 为:

SELECT printf('%04d', product_number) AS product_number FROM table;

将 4 更改为任意数量您需要的数字。这将为产品编号 1 返回 0001。

Since 3.8.3 (2014) You can use printf as:

SELECT printf('%04d', product_number) AS product_number FROM table;

Change the 4 to however many digits you need. This would return 0001 for product_number 1.

┊风居住的梦幻卍 2024-10-23 01:41:17

我知道这是一个老问题,但以下更简单:

--  zero-pad to 4 digits:
select substr('0000'||3, -4);
  • '0000' || 3 将零连接到数字的开头。 SQLite 不介意连接数字,因为它会为您将它们转换为字符串。
  • substr(…, -4) 提取字符串的一部分。 -4 表示从末尾开始四个字符。通常,您可能有第三个参数,但将其省略会选择到最后。

I know this is an old question, but the following is simpler:

--  zero-pad to 4 digits:
select substr('0000'||3, -4);
  • '0000' || 3 concatenates zeroes to the beginning of the number. SQLite doesn’t mind concatenating numbers as it will convert them to strings for you.
  • substr(… , -4) extracts part of the string. The -4 means four characters from the end. Normally, you might have third parameter, but leaving it out selects to the end.
笑看君怀她人 2024-10-23 01:41:17

select substr('0000000000'||'1234', length('0000000000'||'1234')-9, 10) 有效;将 '1234' 替换为您的产品编号,任意长度 <= 10。

select substr('0000000000'||'1234', length('0000000000'||'1234')-9, 10) works; substitute '1234' with your product number, any length <= 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文