Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Reporting with SQL Working with Text Practice Session

Hank Boudreau
Hank Boudreau
804 Points

How to omit ellipses in the Truncate Names problem that are less than 10?

In the Truncate Names problem of the practice session, it asks you to append ellipses to any name greater than 10 letters. I sorted by length of name ascending and noticed that it added ellipses to names that were less than 10 letters. Here's what I wrote

select substr(name, 1, 10) || "..." as "Names", length(name) as "Length" from actors
order by length asc

One of the results is the name "Lu Kai..." and even though this is less than 10 letters (name is just "Lu Kai" in the database) it still adds the ellipses to the end causing it to show "Lu Kai...". How can I stop the query from adding ellipses to names with less than 10 letters?

1 Answer

Steven Parker
Steven Parker
231,184 Points

The code shown above always adds the ellipsis to the string.. You need to code it so this is only done when the length is greater than 10. This is where UNION can be handy, combined with WHERE clause filtering:

SELECT SUBSTR(name, 1, 10) || "..." as Name FROM actors WHERE LENGTH(name) > 10
UNION
SELECT name FROM actors WHERE LENGTH(name) <= 10;