mereth
mereth

Reputation: 495

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?

Upvotes: 8

Views: 15703

Answers (1)

Utsav
Utsav

Reputation: 8093

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.

Upvotes: 8

Related Questions