LEFT, RIGHT, CHARINDEX and SUBSTRING functions in sql server Part 23 — Transcript
Full transcript
- 0:00Hello, welcome to Premium Technologies.
- 0:01I am Wenut. This is part 23 of SQL
- 0:04Server. In this session, we'll talk
- 0:07about a few more commonly used built-in
- 0:10string functions available in SQL
- 0:12Server. Specifically, we'll talk about
- 0:14left, right, car index, and substring
- 0:17functions. And we'll also look at a
- 0:19realtime example of using these string
- 0:21functions. Now, before continuing with
- 0:23this session, I strongly recommend to
- 0:25watch parts 11 and 22 of this video
- 0:28series.
- 0:31Okay. So what is left function? Left
- 0:34function returns the specified number of
- 0:37characters from the left hand side of
- 0:39the given character expression. Let's
- 0:41understand what we mean by this. So
- 0:44select let's say I have a string called
- 0:47abcde EF.
- 0:52Okay. Now what I want to do is I want to
- 0:55retrieve the first three characters from
- 0:58the left hand side of the string. Okay.
- 1:01So I can use the left function for that.
- 1:04Okay. So select left and if you look at
- 1:08the intellisense that is available in
- 1:09SQL server it is expecting um an
- 1:13expression of type and care. So let's
- 1:15pass this there.
- 1:18Okay. And then the next parameter is the
- 1:21number of characters that you want from
- 1:23the left hand side of this expression.
- 1:25Let's say I want three characters. So
- 1:27when we try that, we should get ABC. On
- 1:30the other hand, if you want maybe four
- 1:32characters, just pass in four. You
- 1:34should get also I mean get D as well.
- 1:37Okay. Now, so right is the inverse of
- 1:42left. Okay. Left function returns the
- 1:45specified number of characters from the
- 1:46left hand side of the given character
- 1:48expression. Whereas right returns the
- 1:50specified number of characters from the
- 1:52right hand side of the given character
- 1:54expression. Okay. So if you want let's
- 1:56say def okay then all you do is instead
- 2:01of left you will say right and from the
- 2:04right side let's say I want only three
- 2:06characters. So you get the last three
- 2:08characters which is D E F. So we get
- 2:12that. Okay, so those are left and right
- 2:15functions in SQL Server. Now, another
- 2:18very useful function in SQL Server is
- 2:20the car index function. At first, it may
- 2:22it may sound, you know, a little
- 2:24complicated to use that, but it's pretty
- 2:26simple. Okay. If you look at this
- 2:28function, it has got three parameters.
- 2:31Okay. Um the expression to find,
- 2:33expression to search, and start
- 2:35location. Let's look at an example which
- 2:37makes it clear.
- 2:39Now let's say I have an email address
- 2:41something like this uh maybe sarah
- 2:45aa.com.
- 2:47So a aaa.com is the domain. Now let's
- 2:50say within this email I want to find the
- 2:55index of this at symbol. How do I do
- 2:58that? I can make use of the care index
- 3:01function. Okay. So
- 3:05select I can use the car index function
- 3:08and if you look at the car index
- 3:09function it expects three parameters you
- 3:12know the first parameter is the
- 3:14expression that you want to search for
- 3:16it could be a string or a character now
- 3:18in our case it's a character we want to
- 3:20search the at symbol so I want to search
- 3:23the at symbol
- 3:27and then the next parameter is another
- 3:29expression of type this is nothing but
- 3:32the expression which you want to search.
- 3:34So I want to search for this at symbol.
- 3:36I want to find the index of this at
- 3:38symbol within this given expression.
- 3:42Okay. And then um the third optional
- 3:45parameter is the starting position. So
- 3:47if you look at that the third parameter
- 3:49is the start location. Now do you want
- 3:52to start at the beginning of this
- 3:54expression or somewhere in between?
- 3:56Okay. Now let's assume that you have
- 3:59maybe two or three at symbols in that
- 4:02given expression and you want to find
- 4:04the last one. Okay. So in those
- 4:07circumstances you can use you know the
- 4:09starting position. Okay. So let's say in
- 4:12our case the starting position is one
- 4:15and that's optional. If you don't
- 4:17specify that parameter it's going to
- 4:18anyway start at you know position one of
- 4:21the string. So now when we execute this
- 4:24we should get the index of the at symbol
- 4:26which is five. So if you look at this 1
- 4:292 3 4 5. So the index of at symbol you
- 4:34know is at the fifth location. Okay. So
- 4:36we get the index of that letter. So car
- 4:38index is basically used to return the
- 4:40starting position of the specified
- 4:43expression in a character string. And
- 4:44keep in mind start location is optional.
- 4:48All right. Now another very useful
- 4:50function is substring as the name itself
- 4:52says substring. I want a part of the
- 4:56string. So substring basically returns
- 4:58part of the string from the given
- 4:59expression. Now look at this. If you
- 5:02want a part of a string from the given
- 5:04string then obviously you will have to
- 5:07tell okay the string itself from which
- 5:09you want that part and then where you
- 5:12want to start and how many characters
- 5:14you want. Okay. So those are the three
- 5:16parameters here. the the actual
- 5:18expression where do you want to start
- 5:21and and how many characters you want.
- 5:23Okay, let's see that in action. Now
- 5:25let's say if you look at this um you
- 5:28know email address sarah.com what I want
- 5:31is I want only the domain part. Okay,
- 5:34here the domain is a aaa.com. I want
- 5:37just the domain part. How do I do that?
- 5:39I can use substring. Okay. So, select I
- 5:44can say substring
- 5:47of the expression. The expression is
- 5:49nothing but this email.
- 5:52And then you you want to specify the
- 5:55starting position. The starting position
- 5:57is 1 2 3 4 5. Okay. So, you don't want
- 6:01anything until here.
- 6:03You want, you know, starting at position
- 6:06six. So, I want starting at position
- 6:08six. And then how many characters? 3 4 5
- 6:136 seven characters. So seven. So what is
- 6:16this going to do? This is going to give
- 6:18us that substring from this entire
- 6:21email. So when we execute this, we
- 6:23should get only the domain part. Okay.
- 6:25So these two functions are extensively
- 6:27used car index and substrings especially
- 6:30when you want to manipulate strings.
- 6:33So substring function returns substring
- 6:35part um from the given expression. Okay.
- 6:38Now if you look at the way we have used
- 6:39this function, we have hardcoded these
- 6:42values. You know at which position do
- 6:44you want to start and how many
- 6:46characters do you want. Now if you
- 6:48remember um we want to start at I mean
- 6:52how did you say that we want to start at
- 6:54position six you know because the at
- 6:57symbol starts there. We have counted
- 6:59that and then we specified okay after
- 7:01the at symbol position you know from
- 7:04that position onwards we want the
- 7:07substring okay but this is a little
- 7:09dangerous let's assume you know an email
- 7:12address is of like something like uh uh
- 7:15maybe pam which is only three characters
- 7:18pam u bbb.com
- 7:21so if you look at this email address it
- 7:23has got only three four records now if I
- 7:26pass this name pass this email address
- 7:32to this substring function. Look at
- 7:34what's going to happen. It doesn't give
- 7:35us exactly what we want. We only got
- 7:37bb.com. It knocked off one letter.
- 7:40That's because you want to start, you
- 7:42know, you want the substring from 1 2 3
- 7:454 from the fifth location. Okay. But
- 7:48here it's the sixth location that we
- 7:51have specified. Now when we do this, we
- 7:53get that correctly. So that's the issue
- 7:54with hard coding. So instead of hard
- 7:57coding what you can do basically you can
- 7:59use this car index function which will
- 8:01calculate you know the index
- 8:04dynamically. So you want the car index
- 8:06of whatever.
- 8:09So I want the car index here it is
- 8:11spambb.com.
- 8:13So instead of hard coding that we can
- 8:16just pass the email address.
- 8:18And now when we execute this we should
- 8:20get the same result except that okay and
- 8:24another thing look at this since we have
- 8:26hardcoded that. Okay. And
- 8:30if you look at this one we are actually
- 8:32getting rid of um m and we got this
- 8:36extra letter at symbol there. And to get
- 8:39that because we don't want to start at
- 8:41that position instead we want to add one
- 8:44because the car index of h symbol will
- 8:47give you 1 2 3 4 but you don't want to
- 8:50start at fourth location you want to
- 8:52start at fifth location. So add one to
- 8:55that. So now when you execute this you
- 8:57should get what you expect. Okay. And
- 9:01similarly you can also um you know
- 9:04dynamically calculate the number of
- 9:06characters you want. How how can you
- 9:08calculate the number of characters you
- 9:10want within that string? Look at this.
- 9:12Um you know in this string I I want to
- 9:17start at this location. Okay. And that
- 9:20you have specified using care index.
- 9:22Okay. And the next thing that you have
- 9:24to tell okay from this location how many
- 9:27characters do you want? Okay. Now if you
- 9:30want to get the total number of
- 9:31characters that are available here,
- 9:33don't you think if you get the total
- 9:35length of this string and then subtract
- 9:38the car index of H symbol because the
- 9:41car index of H symbol what will it give
- 9:43you? It will give you 1 2 3 4 and the
- 9:46total length 4 + 71.
- 9:50Okay, from 11 subtract four.
- 9:54Okay, which will give us seven.
- 9:58So
- 10:00we can say okay I want the car index of
- 10:03this one which will give us the car
- 10:06index of this symbol and I want the
- 10:08total length of the string. So if you
- 10:10want the total length of the string what
- 10:12you can do you can use length function
- 10:14and we have spoken about this in the
- 10:16previous session. So length function of
- 10:19what is that of the email
- 10:24and then from that length we want to
- 10:26subtract the car index
- 10:32of the symbol.
- 10:42Okay. So now if we execute this now we
- 10:46don't have those values hardcoded. Now
- 10:48this select statement should work with
- 10:51um any emails.
- 10:54Look at that we get the exact same
- 10:56output. Now at beginning it may sound a
- 10:58little complex. Okay there are a lot of
- 11:00function calls here and it it sounds a
- 11:02lot of confusing but trust me it's very
- 11:04simple if you understand how car index
- 11:07substring and length function works.
- 11:09It's just that instead of hard- coding
- 11:11the values, we are calling one function
- 11:13inside another function. Okay. Now, this
- 11:15forms the basis for this simple
- 11:18real-time example that we have. Let's
- 11:19say I have our employees like this, the
- 11:21first name, last name, and their email.
- 11:23And I want uh you know the total
- 11:28by domain. Okay, for example, in
- 11:31gmail.com, how many how many users are
- 11:33there with gmail.com domain? How many
- 11:35users are there with a.com?
- 11:38Okay, I want to find that and to do that
- 11:41we use exactly the same logic that we
- 11:44have done here but instead of you know
- 11:47hard coding the email address like this
- 11:50we will be passing in the email column
- 11:52from our table. Okay, so let's see how
- 11:55to do that.
- 11:58So when we execute this query we have
- 12:00all the employees here. All we want is
- 12:02an output like this. Okay, total email I
- 12:05mean email domain and the total count.
- 12:09All right, so what do we want in our
- 12:11output? We want the email domain first.
- 12:14So select.
- 12:16So we want the email column in the
- 12:18output. But we don't want the complete
- 12:21email. We just want the domain part.
- 12:24Okay. And remember to get the domain
- 12:26part, we have already written this
- 12:28substring function. So this will give
- 12:31you the domain part. So copy that.
- 12:36But then wherever you have hardcoded
- 12:39this email,
- 12:41we want to pass in the name of the
- 12:44column. It's as simple as that.
- 12:47So email column.
- 12:52Pass it there.
- 13:02Okay. So from where we want from tbl
- 13:05employee table.
- 13:08Okay. So when we execute that we should
- 13:10get all the domains and you want to give
- 13:13a meaningful name for that as email
- 13:16domain maybe.
- 13:18So when we execute that we get the email
- 13:21domain but we are getting all of the
- 13:23domains. Now if you have watched part
- 13:26you know 11 of this video series where
- 13:29we have spoken about group by now it
- 13:31should be pretty simple to group those
- 13:33emails by the domain. Right now you can
- 13:35just group them by this particular
- 13:38column and you're done. So the next
- 13:41thing that we want is the count of
- 13:43employees. count of maybe email
- 13:50as total
- 13:53and then since you are using count
- 13:55aggregate function you want to use
- 14:00group by so I want to group by by this
- 14:05expression.
- 14:14Okay.
- 14:24Uh invalid column name count of email.
- 14:30Okay.
- 14:32So you should get the output that we
- 14:34expect email domain and the total. Okay.
- 14:37And there are other ways of achieving
- 14:39this one. But since we are talking about
- 14:42string functions, I thought it's better
- 14:44to use those functions and achieve this
- 14:46output.
- 14:49On this slide, you can find resources
- 14:50for ASP.NET and C# interview questions.
- 14:53That's it for today. Thank you for
- 14:54listening. Have a great day.
About this transcript
This page contains the full transcript of LEFT, RIGHT, CHARINDEX and SUBSTRING functions in sql server Part 23 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,246 words across 328 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.