问题
I'm trying to get the length of a string in SQL (using Firebird version 2.x+). Whenever I select the length of a string it gives me the actual assigned maximum length of that string as opposed to getting the length of how many of the characters are taken in a record, as you can see here:
as you can imagine, this does not help me, as I can't order by the length, since I'm trying to order by an attribute that has a constant length assigned.
How would I achieve what I am trying to achieve? That is: getting the length of how many characters are taken in a string.
回答1:
As documented for 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 toCHAR[ACTER]_LENGTH
.
The reasons for this is that char
values are padded with spaces to the declared length, so in essence they are of the declared length.
In other words you need to use:
char_length(trim(trailing from imeprodajalca))
来源:https://stackoverflow.com/questions/44855739/getting-the-length-of-a-string-in-sql