G
G
Guzh2017-07-01 10:50:13
PostgreSQL
Guzh, 2017-07-01 10:50:13

Is it possible to cut a string in Select if it exceeds the specified number of characters?

Select id, name, text From Texts
Is it possible to truncate "Texts.text" if it is more than 50 characters and add "..." to it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2017-07-01
@Guzh

select
  case 
    when char_length(some_field) > 15 then substring(some_field from 0 for 15) || '...'
    else some_field
  end
from some_table;

S
Stalker_RED, 2017-07-01
@Stalker_RED

Can.
IF ... THEN
char_length, substring
If length is greater than 50, take the first 47 and add "...".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question