String Functions

16 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.18
List Price:  €95.97
You save:  €28.79
£55.86
List Price:  £79.80
You save:  £23.94
CA$97.87
List Price:  CA$139.82
You save:  CA$41.95
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,267.89
List Price:  R1,811.35
You save:  R543.45
Лв131.37
List Price:  Лв187.69
You save:  Лв56.31
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪259.10
List Price:  ₪370.16
You save:  ₪111.06
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$69,057.73
List Price:  CLP$98,658.13
You save:  CLP$29,600.40
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,776.20
List Price:  ISK kr13,966.60
You save:  ISK kr4,190.40
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,259.44
List Price:  ARS$100,374.93
You save:  ARS$30,115.49
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
Already have an account? Log In

Transcript

Hi. In this lesson, we understand how to deal with string functions. string functions are used with strings, which are nothing but character and character type. Most of these functions are self explanatory. For example, length function returns the number of characters in the string. Lower and Upper are used to convert string to lowercase and uppercase respectively.

In it cap is converting the first letter of every word to uppercase, and the remaining characters to lowercase l pad are pad are used for padding. So they are used to make a string of a specific length. And to do that they padded on the left or right depending on whether you use l pad or l pad with either spaces Or with the characters given. So, fill string there is about what is the character we want to use to fill that extra area. Trimming is done using l trim, our trim and trim we have three different functions l trim is to trim on the left by default it trims all spaces, but you can specify what you want to trim. So, is the case with our trim a trims on the right side called the trailing spaces.

L trim is about leading spaces. But if you want to give a command to trim on both sides, left and right, leading and trailing, then we need to use stream. So those are the three functions we use to deal with the trimming. substring is to take a part of the string What if you want to take Only first three characters of a string are what if you want to take three characters from fifth position, so on. That's what we can do with substring. And substring also allows you to ignore the length.

So you can start taking characters from the position. And everything that is there from the given position will be taken. It's very useful, and we use it in many cases in string is to look for a substring in the mainstream. I want to know whether a substring is present in mainstream. So s one is the main string, s two is the substring. If you don't give anything else we look for first occurrence of s two in s one.

But if you say you're looking for a steel from a specific position, you can give the position here. You can also mention which are currents you interested in, then it'll give you the position of the occurrence that is mentioned. And if the substring is not found in the mainstream, it is going to return zero to indicate that the substring is not present in mainstream as ASCII is to convert the given string to ASCII code, so if I give a string like ABC, it'll give me ASCII code for first letter which is 65. And similarly, see, hey, char is returning the character for the given number. So CH, ar have something like 48 would mean you get dessert zero, because that's the ASCII code for zero. Translate a is converting every occurrence of character given in from to the corresponding character into it is to translate one character to another character.

It's character based translation. When you want to replace a string with another string, we need to use replace, which is a string based replacement. So to better understand them, let's look at some examples. And we use dual tables so that we don't have to worry about any other table. And as your table contains only one row, the output of all these expressions will come for once. So length of Oracle is going to be six because that's what we have.

But what if you have some spaces? Well, spaces are always counted as characters. So if you have any space leading, trailing, or in between, everything is counted. Upper is converting the given string to all uppercase. The same is done with the lower everything is converted to lowercase in it cap is going to convert the first letter of every word to uppercase, and here d will be uppercase. In case if you have something like b A in uppercase, they will be converted to lowercase.

So first letter of every word uppercase, the remaining lowercase l pad is padding the given string to eight characters. It's going to pad it to eight characters by filling the extra space on the left with a star. So I have six characters in that so it has to put two stars on the left and then it has to place a racket because it is L pad means left side padding. But what if I gave only three instead of eight? Then interestingly, it will truncate that on To three characters. So, be careful, if what you give is not large enough to accommodate the whole string, it will be truncated.

L trim is pretty interesting because it can take whatever characters you want to trim on the leftmost. So, in this case, I want to trim all occurrences of zeros and hyphens on the leftmost, so it will keep truncating this, this this and also high fun, because we mentioned that even that is gone. So, it will stop only when it encounters one because that's the first character that is not in the list of characters to be trimmed. So the result of that is going to be just one funny one. And trim as I mentioned, is to trim all stars that are present in the leftmost rightmost position. So all leading starts all trailing stars are gone, because that's what we mentioned.

So we want to trim this from this string, the result is going to be just a racket. And substring will pick up only first three characters, because I said start from one go up to three. Of course, the indexing starts with the one because that is the way it goes in Oracle and the database we start from fifth position. So here is the fifth position, and we take everything from that position. So the result is going to be base. The next one is in string, which is taking the position of the character so where is this a is present here and the result of that is going to be three but in If I give something like x which is not there, you get result as zero.

So if you're looking for something like x, the result will be zero. And this is where I'm looking for a but not from the beginning, but from third position side says start looking for a not from first position, but from third position. So, it will give you the occurrence of a after that so the result is for and this is also the same but we are mentioning things slightly differently. We said we want to start from first position but we are interested in second occurrence not the first occurrence and that is also eventually the same a so the result of both will be for now substring is going to take first five characters from G Up title, we are just using our tables in this example. So, I want to pick up only first five characters and if you don't have five characters whatever is available that will be taken and this is where it is actually taking second word onwards, okay this is what I wanted, I want to take everything from second word ignoring first word, because this is the position from where it should start taking characters.

So, I want to take characters from this position. So look for space plus one and from there, whatever you find, I want to take that so that is about this substring it takes second word on words. And this is where we are taking the first word. This is saying, start at first position and then go up to the Space is going to take everything up to space. But you have some interesting stuff here, what if the job title does not have a space? So please check that and what happens if the job title is not having space?

What will you get from this substring And finally, we use uppercase to get rid of the case differences. And upper of job title is saying convert everything to uppercase, then compare that with this. So we are looking for jobs where the job title contains manager, but we do not mind whether it is in uppercase or lowercase because we convert everything to uppercase to make it case insensitive. The such is now case insensitive. And then we talked about ASCII and character This is giving you the ASCII code for first letter, this gives you the character for the given ASCII code. So, this is a that is 65 and this is where I want to translate every occurrence of star with a dot every occurrence of star is to be replaced with the dot and every occurrence of iPhone is not replace it with anything, we are not giving anything we are giving only one character here.

So, what it does, wherever you have star that will be replaced with a dot. But wherever you have iPhone that will be deleted. So, the result of that is you have starts replace it with the dot but hyphens deleted. So, that's an interesting possibility of course, If you want hyphen to be replaced with underscore or something you can give that here. But in this case, I want to demonstrate how we can delete some characters from a string. And the last function is where every occurrence of leaven G is to be replaced with 18 C. So it's a string replacement, it's looking for a string called Levin, G, and eight is then replacing that with 18 C. So those are the string functions.

Now let's just try a few of them. So here is your SQL Developer, the one that we have been using to understand how to write SQL commands. So here if I say I want to get the length of job title, we are going to get the job title. Okay, there we are, and it's going to give you the length. Well, if your display The job title also then it becomes more clear. That's where you are.

But if you want to take only the first word of that, we have seen how to do that. So substring of job title. And then we want to take the first word, so one comma, and then we say I N str, again, we talk about job title. And we are looking for space minus one to be more precise. So wherever you have sprays, find out the position and go up to the previous character, because we don't want the space to be included. So this is nesting the functions, the value that is returned by this function is used in the expression and that is pass it to another function.

Well, this can be done, go ahead and run and you see something interesting as a totally If you have only one word, that word is not included. The reason for that is you will in string is going to return zero and zero minus one is going to be minus one. That means you're saying take minus one characters from first position, that means you take nothing. So that is something for you to figure out how to fix it. But that's what is happening. Well, if you talk about other functions, for example, if I'm interested in doing some character translation, I can say translate.

And then if I gave as we discussed some iPhones, and then stars, ABC and again some stars, and if I say every star and hyphens should be replaced with a dot and then Something like underscore, then this is going to be what you get. And don't forget to change it to duel, because you want the result to be displayed only once there we are, all stars become dots, all hyphens become underscore. But if you remove this underscore, as we have seen, it's going to remove all occurrences of hyphens, they are gone. So you have only starts replace it with dot, this is character base in replacement or translation. But the same if you try to do with replace, and if you say you would like to replace every star or every double star with something like ABC then you can see this is replace it with ABC.

And this is replace it with ABC because it's looking for just to see Stars and those two stars are to be replaced it with ABC. It's not character based, it's a string based. So what it looks for is exactly two stars. Wherever you find two stars, it's going to replace that with whatever you gave. And as you have seen, the length of the strings need not be same. You can have different lengths for source and replace string.

So those are the string functions. So this lesson is all about how to use string functions. So practice them, get used to them, because we will be using all of them a lot all through. Well, that's all for now. We'll catch you in the next lesson.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.