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 Replacing Portions of Text

Sebastian Eguez
Sebastian Eguez
8,248 Points

Can you explain this string to me?

SELECT * FROM addresses 
    WHERE REPLACE(state, "California", "CA") = "CA";

When I "Run" this, some "states" keep saying "California" instead of "CA". You will see this in the video tutorial as well.

Why is "CA" not replacing "California" completely, in this case?

3 Answers

Kerry Collier
seal-mask
.a{fill-rule:evenodd;}techdegree
Kerry Collier
Front End Web Development Techdegree Student 16,151 Points

It depends on what you're trying to do. So, if you leave the WHERE off of the initial, it will do what you were thinking initially, find all the instances of California and replace it with CA and display it that way. The WHERE Replace doesn't do anything at that point as it has already changed that in the query results.

If you do want to get the state displaying how it does and only select specific columns, you can just SELECT street, city, state, zip FROM addresses WHERE REPLACE(state, "California", "CA") = "CA"; and it will work the same as how the example does.

Basically, what it all comes down to is how you want to display the results of the Query. When displaying to a user, you may just want it to show all of them as CA, but there may be a reason you want to see the actual original data if you're just doing the Query for yourself.

To be honest, there's a few ways to get the results based on the data in the example, as it's a pretty simple example. But, it becomes more useful when the Query is more complicated. For instance, if I wanted to see all the ones that are listed as California and CA, I would likely just do a SELECT * FROM addresses WHERE state IN('California', 'CA'); This returns the same thing and is obviously less typing, but again, it really depends on what your situation is.

It's a good thing to keep in the tool box in case you ever run into something that might be better served by a Query like that. One of the nice things about SQL, once you get the hang of it, is that it can be pretty flexible on how to get results, and some ways will be better than others depending on the situation. I hope this helps some!

Kerry Collier
seal-mask
.a{fill-rule:evenodd;}techdegree
Kerry Collier
Front End Web Development Techdegree Student 16,151 Points

So, what is happening here is that the SELECT, how it is written, is looking at the data, internally taking the instances of "California" and changing them to "CA" and comparing that to the ="CA" after the parenthesis then returning all rows that = "CA". So, it's not going to replace what is returned in this case, it's just changing them virtually so that it can match it to the value "CA". Now, what is being done in the code challenge after this will do what you're expecting to see, but it's written differently, as that is a SELECT REPLACE as opposed to a SELECT FROM. I hope this makes sense!

Sebastian Eguez
Sebastian Eguez
8,248 Points

When writing

SELECT street, city, REPLACE(state, "California", "CA"), zip FROM addresses 
    WHERE REPLACE(state, "California", "CA") = "CA";

Do you still need...

  WHERE REPLACE(state, "California", "CA") = "CA";

...again?

Yes you need to keep the WHERE block of code or else you won't get all the states that have 'CA' you'll be getting all the different states that are in the database. :)

SELECT street, city, REPLACE(state, "California", "CA"), zip FROM addresses WHERE REPLACE(state, "California", "CA") = "CA";