So that's the basics.
But there's another wild card character that I'll show you, which is the underscore.
So here I have a really weird query.
This is for underscores in a row.
You just have to trust me that it's for.
And it was hard to tell what this will do.
So we're searching stock quantity, which is a number, and we're trying to find where it's like for
underscores.
And basically an underscore is a wild card to specify exactly one character, any character, but exactly
one.
So four of them in a row is saying, I want to find where the stock quantity is.
Four characters long.
So let me show you why we might want to do that.
If I do a select stock, let's do title and stock quantity from books.
Just like that.
You can see that we have stock quantity of two or sorry, two digits.
Two digits, 30 to 43.
Three digits.
Geez, it's given me a tough time.
Digits, 154 And then this great book linking in the Bardo has 1000 copies in stock is brand new.
People don't know about it yet.
They're not buying it.
I'm sure people will buy it soon, though.
Once, of course, they realize what a great book it is.
So if I wanted to select books where stock quantity is four digits long, I could use that wild card.
So let me just show that again.
So select title and stock quantity from books where stock quantity like and all I have to do.
So just to clarify, if I did this, that wild card just means anything.
So that's going to give us everything.
And if I do four of them, it doesn't make a difference.
That's not saying four characters.
That's just four different ways of saying anything at all.
It could be nothing.
It could be 1000 characters.
It doesn't matter.
So by adding underscores, that's for there.
You'll see I get Lincoln in the Bardo or I could do two underscores and I only get the two digit stock
quantities.
Now, that's not to say the only time you use underscores is when you're trying.
Yes, Blue, it's my cat.
Are you going to say hi?
She's now sitting on the computer.
Okay, so back to where I was.
Get this cat hair out of here.
The underscore is not only used and actually it's not even primarily used for counting digits like that,
but it's a way of specifying exactly one character.
Like I said, let's say that we're working with phone numbers and in the US they might look something
like this.
So we have an area code in parentheses and then three digits dash for digits.
And if we wanted to match that pattern, we would have something that looks like this like and we'd
have our first parentheses.
And if we just put this wild card there that's going to say any number, any letters, whatever it is
in between those parentheses.
But we're actually looking for three digits and then another three digits after that parentheses, then
a dash and then four digits.
So basically we have these two wild cards at the end of the day, present sign, which means anything
or nothing and then underscore which is exactly one thing and you can chain them together to match patterns.
Then that brings up another question, which is, but what if I'm searching for a book that has a percent
sign in it?
How would I do that?
And you may have noticed that I expertly planted a book title with a percent sign in it.
I was thinking ahead for once, how would we select that?
Or there's also a book title that's not so creative that has an underscore in it.
How would I select that one?
If these characters are special wild card characters, how do I actually search for the exact character
percent sign or the exact character underscore?
And the answer is actually pretty straightforward.
We use what's known as an escape character.
In this case, I always forget which one's backwards and forwards slash but whichever slash, this is
the one over the enter key.
Pretty sure that is backslash.
So backslash percent sign is going to match a percent sign.
So this entire thing right here is basically saying percent sign.
Likewise with underscore, it's backslash.
Underscore is the escape character.
So let's put that into practice, let's try it out.
So we'll select title from books and we'll just do that first of all.
And you'll see we've got 10% happier and then fake underscore book.
So let's start with 10% happier.
So I want to select title from books from books where title like and just I mean, this should be clear
by now, but if we try to do this, that's not going to work.
That's going to give us everything.
But if I tried to say, okay, percent sign, that means anything, and then a percent sign and then
anything afterwards, that's not going to work either because that's basically just three different
ways of saying anything at all in the title.
So that's where we need our backslash.
And now you could read this as you could condense this to say exactly 8% sign, one percentage sign,
anything before, anything after.
And there we go.
That's how we get 10% happier.
Same thing with the underscore.
I won't go through that whole routine again.
We just replaced that backslash percent with backslash underscore and that gives us fake book.
All right.
So that's all that there is to like for now.
It's very, very useful.
We'll be using it quite a bit in this course.
It's probably one of the most commonly used things in my SQL or in SQL when doing queries.
In all likelihood, you won't be matching percent signs or underscores all that often, but you'll be
doing things like this all the time where you're using the wild card and the underscore wild card to
match patterns.
Next up, exercises.