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

Development Tools

Helen Brookes
Helen Brookes
7,549 Points

Select the first letter of the "first_name", followed by a period, followed by a space and then add the "last_name". Als

Hello,

Totally stuck on Database Foundations String Functions Task 3. Where DOES the substring go?

Thanks!

2 Answers

Jacob Tollette
Jacob Tollette
27,884 Points

This is 8 hours later, but if you're still having the problem (or anyone else for that matter), here's an example:

    CONCAT(SUBSTRING(), ".", UPPER()) from users

The MySQL Reference Manual has some other great examples.

Try not to think about it like "Where does the SUBSTRING go" and try to break it down to a simpler problem. You need to get from this: John Smith to J. Smith. Knowing that SUBSTRING takes a portion of a string, break it down. You need to use SUBSTRING on the first_name column to abbreviate the first name.

    SUBSTRING(first_name, 1, 1)

From the reference manual, we see: SUBSTRING(str, pos, len) where str is the string, pos is the starting position (MySQL is 1 indexed), and len is the length of the substring we want.

Now that the SUBSTRING is worked out, we just need to think about where to place it. You know that adding strings together is called concatenation and is done with the CONCAT function. This function simply sticks strings together.

Let's say there's another column called "middle_name" and we want to go all J.J. Abrams, here's how we could do that with CONCAT and TWO SUBSTRINGs:

    CONCAT(SUBSTRING(first_name, 1, 1), SUBSTRING(middle_name, 1, 1), last_name)

Break it down by typing it the way you want it to look in the end and then go through it step-by-step.

    CONCAT("j", "j", "abrams")
    CONCAT(SUBSTRING(first_name, 1, 1), "j", "abrams")
    CONCAT(SUBSTRING(first_name, 1, 1), SUBSTRING(middle_name, 1, 1), "abrams")

I hope that all made sense and was in time to help. Also, if I'm wrong about anything let me know. I love to learn from my mistakes.

NOTE: I know I didn't put the "."s in the final example. I wanted it to be readable.

Helen Brookes
Helen Brookes
7,549 Points

Thank you very much indeed Jacob! You'll be pleased to know that I've now made it a habit to break things down and think them through rather than simply trying to remember code syntax. It's a phenomenal breakthrough for me...I've since used this approach on subsequent challenges...and it works!

Much, much appreciated!

Helen