I welcome the module force. Equal programming to this is less than one sequel functions were currently in sub lesson, one point or string functions. And the previous lesson. We got to work with date functions a little bit and in the next lesson will actually define our own functions
for this lesson. We're going to, ah, deal with a problem that comes up from time to time. We're gonna pretend that our database has one name. Field, It doesn't have a first name field, it doesn't have a last name field. And now, for whatever reason, be it a report or a system transfer. We now need to move the first name and the last name to the new system,
which means we have the parts that name field.
For this lesson, we're gonna use the ACU Matic, a database. We're going to use the B account I or the B account table, and we're gonna pretend that a Q Monica doesn't have a field for the first name and last name. Even though it does on another table, we're gonna pretend that we have to parse out the name. So
that's that. Let's go ahead and get started
So make sure you have a queue. Magica.
So make sure you have vacuum. Atticus. Set as your default database, Go to your sequel editor, new sequel editor
and let's start typing. So we're gonna select. I don't know what I want that someone dropped down to my from statement from which table do I want? I want to be accountable.
Okay, Amber say, because I know this. I happen to know this through my experience that the type that we want is e p
for employees because employees have a name.
Let's see what we get. Go first. Let's add a calm so account name. Let's run this to see what we get.
Yep, that looks like a bunch of names. Let's see what would happen if we, uh, shows everything except employees from this table.
Yeah, looks like we're getting a bunch of different types and names from businesses to states.
So let's get back to the employees we don't want apart. We're parsing names for people,
so let's get back to the people.
Now let's take a look at a couple functions that we might use to accomplish this. First, we're gonna need the locate function so locates.
There's actually another way to do this. But we're gonna do it this way because this will show you the utility of the functions. Offer locates
So the first argument is the comma. The second argument is the field of using
I need a comma here, too. Don't forget that
as index Index is a special word. So put a dash or a not a dash, but am underscore at the end.
Let's go ahead and run this and see what we get back.
Okay? So if you look at this first result, we'll see 16. What that means is this comma is at the 16th position of strength.
Okay, so that's locate. Locate is a function. It does something very specific.
The specific thing that it does is it finds the index of the character, which is the first argument. And the string is the second argument.
The next function we want. Take a look at this sub string. You'll need to use that.
Now we need to pass in through the string that we're using, which is account, name and one. We're gonna start at one, and we're gonna go Three spaces,
Well, say, uh, last name
is the alias is going to run that
it's not really the last time we're gonna see what it does. So we have the string,
the starting position and how many characters to pull out so we can see that it started at the first position, which was an A, and pulled out three characters, which was a nd It's exactly what we expected to happen. So how is this going to work? Well, we're gonna use the locate function to define
the ending position of the substance because we're gonna pull out this first part of the name
and we're gonna use the locate function to provide the stopping point.
So let's do that. Let's copy this.
And it looks like they're all
the limited by a space. So let's change this comment to a space.
Let's go in and run this to see what we get.
I got a couple commas on the end there.
Um, that's because we're getting the last character. The last character for a lot of these is actually a space that we do not see.
Um, because it looks like all the white space. That's after it anyway. But it's there.
And then when it's not, it's a comma. But we don't want that comma.
So let's do a minus one here. So this will subtract one off off the character count to pull out. And it should pull off that last space for these guys in that last comma for that guy.
Let's go ahead and run that.
so here's here's, ah, one of the problems that we're hitting. Um, this guy actually has a comma and a space as the ending
of his string, so we actually can't just do minus one. We'd actually have to do minus two to fix him, but that will ruin the other guys by chopping off there
their last character in the name. So
let's use the replace function.
What is replace do well, It's a function into something very specific.
It replaces a character with another character, so our first argument is the answer that we just got from running the sub string with the locate. Our second argument is going to be a comma,
and our third argument is going to be empty or nothing. So we're going to find the comma
in this thing that results
and replace it with nothing. And we're gonna call that our last name.
And it looks like we have it. Okay, so we got the last name. We used a combination of three functions the locate, the sub string and the replace.
All right, so let's move on to the first name
and let this be a early lesson. If you're gonna create a database with name field, make it sure. You get that last name and the first name in there as their own fields to get filled in because it always comes up. Where? For some reason,
either a manager wants the first name and last name broken out. Or you're transferring to a system where you have to provide the first in the last name.
And if everyone is typed it in, they've all used different ways. They've done last name. First name, first name, last name, comma spaces, you name it. This is actually kind of consistent.
More consistent than if users were just typing it in.
So let's see. We want to get the first name now.
So what? We're going to d'oh
of the account name again.
we're going to use the sub string function with the accountant and field, and we're going to pace, then
with the space in the account name.
Well, go ahead and run that
now. We actually have an extra space at the front of the front of the string, so let's get rid of that by adding one to it.
Okay, so you may have noticed that when this sub string we actually used to arguments when in the top version, we actually used
three arguments we had the field to start and the end and this one, we just have the field on the start.
And that's because the sub string function in my sequel makes the assumption that if you have not provided the third argument, it's because you just want to go to the end, Which is why we're getting this data back.
again, we just want the first name. So we actually don't want the comma space, Mr that we're seeing.
So to make this simpler, we're actually gonna start to break it apart using some tools that we learned previously. The table expression as first piece.
All right. And then we're also gonna pull out thea
index off. So let's do it
We want to locate the comma
this above string. So we're just gonna copy and paste
and it's good and run that.
So this is at the eighth place of that name That looks right.
Joseph was already broken out because he didn't have a, uh,
Um, so his count is zero. All right, so we're gonna use this.
So we're gonna say with table expression time
name chunks, Nicollet name junks
make sure we get this closed off.
Let's go ahead and select star from Maine chunks to make sure that everything's working so far.
Okay, It looks like it is. So we're gonna start to work with this to get the first name out.
So we already have the last name.
All right. And now we need to work with the first name again. Now, the reason I turned this into a table expression of past it and layer down is because we're starting to get a long function chain, and those could be hard to read. So when they start to get too long for me, sometimes I start to break my concerns apart. I start to
spread out the complexity so that it doesn't turn into one giant long function, Shane,
and that makes it easier to read.
So what do we need to do? Well, there's some names where we don't want to work with it. We just want to pass the bac. So when position,
we use a case statement here now cases, Justin, if Dan statement for sequel. So its case.
What's the case when position
we just want the first piece.
Let's just end this.
Make sure this is working correctly,
Okay? So when and this seems to be working correctly, we're getting a first name
when there's no comma found. Okay, but that doesn't complete it because he only works for some people. So we need an l statement. So it moves and down to say else
do the sub strain function.
We're going to start at position one
and we're going to go to the position
Let's see what we get back for this. Sometimes you gotta move piece by piece. No pun intended.
Okay. Looks like we're almost there. We just have one problem. We have the comma at the end.
Well, let's go ahead and do minus one.
So this is going to be that numerical index position minus one.
And there you go, folks. I'm sorry this lesson started to drag out, but I wanted to show you some of the challenges you can face and how you can solve them using functions such a string functions and Channing to get them together. Thio
go over. What? We went over this lesson. We used a series of functions and table expressions to break out the first name and last name from a field
where it was the name combined into one. Sometimes it had a Suffolk. Sometimes it didn't, you know, sometimes had Mr or Mrs sometimes it didn't.
And what we did is we used the locate with the sub string,
and then we made a table expression when it started to get too complicated to bring it down another layer
and Then from that table expression,
we selected the data we were interested in.
We used a case statement, which is just an if dan statement for sequel, um, to decide whether or not we wanted to work with that
first name piece or not.
And at the end of the day, we achieved our mission is we were able to parse out
the last name and the first name. I hope you got that working and feel free to go ahead and practice because you know the old saying practice makes perfect. When you're new to this kind of stuff, this function chaining, it can look kind of weird and foreign.
And, you know, for a lot of people, that's just because it is. But once you see what it's working piece by piece, it starts to really fall in the place on how to use this stuff to your advantage to achieve what you want to achieve. Anyways, I hope you enjoy this lesson and I hope to see you in the next. Thank you