简体   繁体   中英

Getting the length of a string in SQL

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.

As documented for char_length :

Notes

  • With arguments of type CHAR , this function returns the formal string length (ie 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 .

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM