Getting the length of a string in SQL

谁说胖子不能爱 提交于 2021-02-05 08:42:19

问题


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


来源:https://stackoverflow.com/questions/44855739/getting-the-length-of-a-string-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!