获取 SQL 中字符串的长度

Getting the length of a string in SQL

我正在尝试获取 SQL 中字符串的长度(使用 Firebird 版本 2.x+)。 每当我 select 字符串的长度时,它都会给我该字符串的实际分配的最大长度,而不是获取记录中包含的字符数的长度,如您在此处看到的:

如您所想,这对我没有帮助,因为我无法按长度排序,因为我试图按分配了固定长度的属性排序。

我将如何实现我想要实现的目标?即:获取一个字符串中取了多少个字符的长度。

根据 char_length 的记录:

Notes

  • With arguments of type CHAR, this function returns the formal string length (i.e. the declared length of a field or variable). If you want to obtain the “logical” length, not counting the trailing spaces, right-TRIM the argument before passing it to CHAR[ACTER]_LENGTH.

原因是 char 值用空格填充到声明的长度,所以本质上它们是声明的长度。

换句话说你需要使用:

char_length(trim(trailing from imeprodajalca))