glarkou
glarkou

Reputation: 7101

Truncate and add ... if string is more than x characters

We would like to trunate the string if it is longer than 100 and add ... at the end.

We would like to do the whole operation on the DB to avoid network cost.

We use substring(s, 1, 100) but how we can combine it with a function (most optimal function) to check and add the ... if the string is more than 100 characters?

Upvotes: 0

Views: 621

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269953

You can use:

(case when length(s) < 100 then s
      else substring(s, 1, 97) || '...'
 end)

Upvotes: 2

Related Questions