found a slightly more elegant solution:
SELECT greatest(colname,rpad(colname, 2));
eg:
SELECT greatest('foo',rpad('foo', 5)); -- 'foo ' SELECT greatest('foo',rpad('foo', 2)); -- 'foo'
.
To explain how this works: rpad ('foo', 5) = 'foo', which has the value '' foo '' (the largest number works with both strings and numbers) rpad ('foo', 2) = 'fo', which is equal to <'foo', so 'foo' is selected by the largest function.
if you need left words that you cannot use anymore because they are compared from left to right (for example, "oo" with "foo"), and in some cases it will be more or less depending on the string. I assume that you can change the line and use rpad and cancel it back, or just use the original solution, which works in both cases.
source share