Next up, we move on to another string function called replace.
So replace allows us to replace portions of a string with some other replacement string.
Now, like all of these functions I've shown you so far, we're not actually updating the data, at
least the way that we're using them right now.
We're not updating our tables at all.
We're simply displaying or selecting information and changing the way that information looks after we
remove it or we select it from the database.
But our data is completely unchanged.
And that's important to note, because some people think, you know, the word replace means that we
are replacing the contents of our table, that we're actually updating rows.
But that's not the case, at least with what I'm showing you right now.
So as always, I recommend pull up the docs, find the examples, replace or replace.
There you are.
It's a pretty simple one.
We have to provide three different string arguments.
The first one is the string that we will be operating on.
The second one is what we want to replace and the third one is what we want to replace it with.
So in this example, this is the initial string.
This is what we're trying to replace and we're replacing it with an uppercase W, lowercase W So instead
of w w w we now have upper w, lower w, upper W lower, upper and lower.
So let's take a look at an example here.
What do you think I end up with when I run this line?
Select Replace Hello world.
Hell and then four characters.
Well, remember, this is what we are operating on.
This is what we're replacing.
So this h e l will be replaced with this last argument.
So we get this.
BLEEP o world.
Let's run it just to prove it over in my shell.
I'll paste it in.
And there we are.
We see bleep o world.
Okay, now, how about this one here?
I'm on two lines.
Just because it was too long.
The font was tiny, but there's no reason to do it on two lines.
Really.
Replace cheese, bread, coffee, milk.
And then what are we trying to replace a space character and what are we replacing every space with?
SpaceX and SpaceX.
So essentially, any time there is a SpaceX, it's going to be replaced with SpaceX and SpaceX, meaning
we end up with cheese and bread and coffee and milk.
I'll prove that one as well.
Paste it in.
And there we are.
Cheese and bread and coffee and milk.
So that's all there is to using replace, at least on its own.
Right, without a table, without any real data behind it.
It still works the same way, but there's no fancy options.
There's no different ways of calling it.
You always have to have those three values.
The initial string, the thing you're trying to replace and the thing you want to replace it with.
If so, those three values must always be there.
And another thing you should know is that it is case sensitive.
Let's do an example here.
Let's select replace.
Whoops, I accidentally hit enter.
You know, that's going to be confusing.
Then we just terminate the string and clear my screen and start this over.
All right, go away.
Control, By the way, we'll clear the screen.
So select replace.
I am cold.
How about that?
I'll put an exclamation point there too.
And then I want to replace my name.
So if I do it this way and I don't capitalize it correctly and I replace it with, I don't know, Mr.
Steel like that.
We still see.
I am Colt.
It's completely unchanged.
Again, the casing matters.
If I try and replace see Capitol Colt, then it works.
I am Mr. Steel.
So let's try an example now with our books table.
What should we do with our books?
Select star from books.
We've got these spaces in the names of a lot of the books Norse mythology, American Gods, Interpreter
of Maladies.
Why don't we try replacing all spaces with hyphens or dashes?
So I'm going to do a select replace the title of every book.
We want to replace a space.
Let me use single quotes.
And replace it with a dash.
I don't know why we want to do that, but let's say that's what we care about.
And I'm an idiot because I didn't say from books.
And there we are, the namesake Norse mythology, Right?
What we talk about when we talk about love.
Great short story collection, by the way.
Anyway, lots of spaces replaced with dashes.
So that's one example.
We can get even fancier.
I'm not going to bother with it right now.
I'll show some more complicated examples later.
But we could do the same thing that we did where we combined cat and substring.
We could combine replace with any of them.
I could replace all the spaces with a hyphen and then concatenate or we could concatenate the first
name and the last name and then replace spaces like David Foster Wallace has a space in his last name,
but I think I want to keep this moving along.
So that's it for Replace.
Just remember the way that it works.
Three arguments.
All of them have to be there.
The first one is the piece of text we're operating on.
The second one is what we're trying to replace, and the third one is what we want to replace it with.
And I can't stress enough, we have not altered anything in our table if I do a select title from books.
Nothing has changed.
We still have spaces there.
We're not updating the table at any point.
All that we did was retrieve the titles and replace them once we had retrieved and replace the spaces
with dashes.