Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
217,443 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;