mereth mereth - 6 months ago 20
SQL Question

SQL truncate length of a string

i have a table programmers (id, name) in postgreSQL and i want to return names of all programmers and if a name has more than 12 characters i want to truncate it to exactly 12 characters, any ideas how can i do that?

Answer

Try this

select substring(name from 1 for 12) from your_table

Referred https://www.postgresql.org/docs/9.1/static/functions-string.html

Or the comment mentioned by @Prdp is a better alternative I think. Check the cost for both and use the one suits you.