Replicate, Space, Patindex, Replace and Stuff string functions in sql server 2008 Part 24 — Transcript
Full transcript
- 0:00hello welcome to pre Technologies I am
- 0:02wkit this is part 24 of SQL server in
- 0:06this session we'll learn about the rest
- 0:08of the built-in string functions that
- 0:10are available in SQL Server specifically
- 0:13we'll be looking at replicate space Pat
- 0:16index replay and stuff functions before
- 0:20continuing with this session I strongly
- 0:22recommend to watch Parts 22 and 23 of
- 0:25this video
- 0:26series all right the first function that
- 0:28we'll look at is the replicate function
- 0:31now if you look at the syntax of the
- 0:33replicate function it has got two
- 0:35parameters string to be replicated and
- 0:38how many times you want to replicate
- 0:41that given string so this function
- 0:43repeats the given string for the
- 0:45specified number of times let's
- 0:48understand this with an example if you
- 0:50look at here I have the select Clause
- 0:53select and then we have this replicate
- 0:55function and to this we are passing this
- 0:57Pim keyword that's the first parameter
- 1:00and the second parameter how many times
- 1:03you want to replicate this I want to
- 1:04replicate this three times so when we
- 1:06execute this praim word along with the
- 1:10space next to it will be repeated three
- 1:13times okay now here is a simple
- 1:15practical example of the replicate
- 1:17function if you look at this we have got
- 1:20first name last name and email of the
- 1:22person now what we basically want to do
- 1:24we have seen in many of the realtime
- 1:26applications they don't reveal full
- 1:28email address uh but maybe they will
- 1:31just reveal that you know using some
- 1:33masks um you know uh so basically we are
- 1:37masking here the email address of a
- 1:39person using five star symbols and to do
- 1:42that we are using the replicate function
- 1:45and using the replicate function itself
- 1:47is pretty simple here look at this in
- 1:49the output we want the first name last
- 1:50name and you know the mask email so
- 1:54first name last name we will select it
- 1:56as usual but we want to mass the email
- 1:58column so first what we are doing we are
- 2:01getting the two the first two letters of
- 2:04the email okay and obviously if you want
- 2:07a sub string of a given string we use
- 2:10the substring function and we have
- 2:12spoken about substring function car
- 2:14index length in the previous session in
- 2:17a great detail so if you want to know
- 2:20how to work with that please watch the
- 2:21previous session so this first piece
- 2:24here it gives us the first two letters
- 2:26of the email and to that two letters we
- 2:29are actually con catenating using the
- 2:31plus sign five star symbols look at this
- 2:34this is where we are using the replicate
- 2:36function I want to replicate this star
- 2:38five times okay and then to the five
- 2:42stars we are then appending the add
- 2:44symbol and the domain okay aa.com or
- 2:48bbb.com whatever okay and again to get
- 2:52the add symbol and the domain we are
- 2:54using the subring because we want the
- 2:56rest of that okay so we are basically
- 2:59using replicate function to mask the
- 3:02email one of the practical uses of
- 3:04replicate
- 3:05function all right the next function
- 3:07that we'll talk about is the space
- 3:09function and this is a pretty simple
- 3:11function to understand okay now if you
- 3:13want a space in a SQL query you know you
- 3:16basically can use um you know something
- 3:20like this so if I just want a
- 3:23space I can say
- 3:26select and you know within the single
- 3:28code we can specify how many other SPAC
- 3:30spaces I want for example if I want five
- 3:33spaces 1 2 3 4 5 I have all the five
- 3:36spaces there and it shows I mean
- 3:38obviously you can't see a space but
- 3:41that's how it is now this is a little
- 3:43odd let's say if I want like 10 spaces
- 3:46this becomes you know little odd to look
- 3:48at that's why using the space function
- 3:51is much better so when we say select
- 3:54space of maybe how many ever you want I
- 3:57want five space and this is more
- 3:58readable as well now in the first case
- 4:01you have to literally count 1 2 3 4 5
- 4:04you have to do that I mean here by
- 4:06looking at that you can say okay there
- 4:09is going to be five spaces now if I
- 4:11execute this line okay obviously we
- 4:13can't see them
- 4:14but okay so what we basically want to do
- 4:18with the space function is um you know I
- 4:20want to insert you know five spaces
- 4:23between the first and last name you know
- 4:25I want the full name of the employee but
- 4:28between their first and last name name I
- 4:30want five spaces how do I do that we
- 4:32just select the first name concatenate
- 4:34that with five spaces using the space
- 4:36function and then their last name and
- 4:38give it an alas name full name from
- 4:40table employee pretty
- 4:43simple and obviously if you want to do
- 4:45the same thing instead of using the
- 4:46space function you can literally put
- 4:48five spaces within single quotes here
- 4:51but then that's not that
- 4:55readable okay so the next very useful
- 4:58function that we'll talk about is the
- 5:00pat index standing for pattern index now
- 5:04this pattern index function Returns the
- 5:07starting position of the first
- 5:10occurrence of a pattern in a specified
- 5:12expression so in the in the given
- 5:14expression okay it will find the first
- 5:18occurrence of a pattern that you want
- 5:20okay in a way Pat index is similar to
- 5:24car index look at that pattern index
- 5:27character index now both of them return
- 5:30you know the index of a string but with
- 5:33Pat index you have the flexibility of
- 5:35using wild cards but with c Index you
- 5:38cannot use wild cards that's the only
- 5:40difference and both of them are
- 5:42extremely
- 5:43useful okay so here is a practical
- 5:46application of this pattern index now
- 5:48what we are basically doing here is we
- 5:50are finding the look at the query here
- 5:54so we're getting the email and within
- 5:57the within the email I am finding this
- 6:00pattern you know at aaa.com I want you
- 6:04know the index the start index of at
- 6:09aaa.com okay before that I don't care
- 6:12what what is there I mean do we have
- 6:14three characters four characters is it
- 6:16Sam Ram John whatever we don't care
- 6:19that's why we are using the wild cards
- 6:21again we have spoken about wild cards in
- 6:23a great detail in the previous sessions
- 6:24of this video series so please check
- 6:26back if you don't know what are wild
- 6:28cards and how to use them okay so we are
- 6:30specifying okay you know I want the
- 6:34pattern index of this at aa.com but I
- 6:39don't care what is really before that
- 6:42okay before that add symbol and then we
- 6:45are using so I want to find this pattern
- 6:48in this email column of the employee
- 6:50table okay so obviously when we execute
- 6:54that query you should
- 6:56see so if you look at this without I'm
- 6:59executing this query without the wear
- 7:00Clause so when I execute without the
- 7:03wear Clause look at this wherever we
- 7:05have
- 7:06aaa.com at
- 7:08aaa.com you know we are getting the
- 7:10index there the first occurrence the
- 7:12first occurrence here is 1 2 3 at the
- 7:15fourth position we get that but look at
- 7:17this here we got for at
- 7:21ccc.com if you look at this string we
- 7:24don't have at
- 7:26aaa.com string within that email mail so
- 7:30that why that's why it returns zero so
- 7:32if the given pattern is not found in the
- 7:34given string then it returns zero okay
- 7:38and so obviously if you just want the
- 7:41emails for which you have this index
- 7:44number then obviously you can use a wear
- 7:46clause and filter that with greater than
- 7:49zero and it will only give us those
- 7:51emails which has got that pattern we
- 7:54want which is at
- 7:57aaa.com so that's about Pat index and
- 8:01and it's very much similar to car index
- 8:03as I told
- 8:05you all right the next function that
- 8:07we'll talk about is the replace function
- 8:10as the name says it's going to replace
- 8:11something for us so it basically again
- 8:14this function is really useful we
- 8:16replace all occurrences of a specified
- 8:19string with another string okay so I
- 8:22have the string you know expression and
- 8:25a pattern and a replacement value okay
- 8:28with in this original string
- 8:29whatever pattern you specify okay we
- 8:32want to replace that pattern with this
- 8:35value okay so if you want a very simple
- 8:37example look at this we have got first
- 8:39name last name email now what I want to
- 8:41do is I want to replace the with net
- 8:45it's pretty simple to do all you do is
- 8:48okay use the replace function obviously
- 8:51so for the replace function you specify
- 8:53in the email column I want to
- 8:58replace with with.net it's as simple as
- 9:01that and give it to Alas as converted
- 9:03email from table employee let's see you
- 9:06know this query in action again these
- 9:09functions are very simple to use all you
- 9:11have to understand about is the
- 9:13parameters that it
- 9:17expects so let's execute this
- 9:21query you know the email and the conver
- 9:24Emil Dooms got converted into
- 9:27net okay so the final function that
- 9:30we'll talk about uh is the stuff
- 9:32function um you know as the name States
- 9:35it's going to stuff something okay uh so
- 9:38basically what this function does is
- 9:41it's slightly different to replay I mean
- 9:43it's going to replace some characters
- 9:45but then look at this the stuff function
- 9:48it expects a
- 9:50string and then within that string at
- 9:53the start position you specify and for
- 9:57how many characters the length okay okay
- 10:00and the replacement expression so what
- 10:02what what the stuff function going is
- 10:04going to do is you can say okay start at
- 10:07position two if you look at the way we
- 10:09have used the star function here I mean
- 10:11it's very clear here uh first let's
- 10:13understand the output that we want you
- 10:16know we want the first character from
- 10:18the email okay we want to leave it there
- 10:21but then for the
- 10:24next you know three characters after the
- 10:28first character for the next three
- 10:30characters I want to replace the next
- 10:32three characters you know in this case
- 10:34am and add symbol I want to replace that
- 10:37with five stars and then whatever
- 10:40follows that we just want that okay um
- 10:44so basically this is another kind of
- 10:47masking okay uh using the stuff function
- 10:50and look at this if you look at the way
- 10:51we have used this stuff function uh we
- 10:54are saying okay work on the email column
- 10:57on the email column start at letter two
- 11:01okay which means leave the first
- 11:02character but start at number two and
- 11:05then for the rest three characters you
- 11:07know if you take Sam at a.com okay start
- 11:10at two and the next three characters a m
- 11:13and at for these three symbols replace
- 11:17those three characters with five star
- 11:21symbols okay and then obviously we are
- 11:23giving it an alas as stuffed email and
- 11:26we have that there okay so basically
- 11:29stuff function inserts this replacement
- 11:32expression at the start position
- 11:34specified along with removing the number
- 11:38of characters that you have specified
- 11:40using the length
- 11:42parameter on this slide you can find
- 11:44resources for asp.net and cop interview
- 11:47questions that's it for today thank you
- 11:48for listening have a great day
About this transcript
This page contains the full transcript of Replicate, Space, Patindex, Replace and Stuff string functions in sql server 2008 Part 24 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,939 words across 274 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.