Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,390 --> 00:00:04,900
In this lesson, we're going
to be looking at functions.
2
00:00:04,900 --> 00:00:09,460
And functions are something
that are a very powerful way
3
00:00:09,460 --> 00:00:11,380
to do data processing.
4
00:00:11,380 --> 00:00:15,550
Specifically, we'll be looking
at single row functions.
5
00:00:15,550 --> 00:00:17,980
Single row functions
are designed
6
00:00:17,980 --> 00:00:22,330
to do a certain
operation on every row
7
00:00:22,330 --> 00:00:24,950
in a particular
column of a table.
8
00:00:24,950 --> 00:00:28,400
So let's get connected.
9
00:00:28,400 --> 00:00:29,770
And we'll look at
our table list.
10
00:00:32,590 --> 00:00:35,740
And let's do a simple query.
11
00:00:35,740 --> 00:00:43,060
Select ename from the
emp table and execute.
12
00:00:43,060 --> 00:00:47,850
So here, we see the ename column
with employee's last names
13
00:00:47,850 --> 00:00:50,810
that are stored in the database.
14
00:00:50,810 --> 00:00:53,390
But let's say that
they're in uppercase,
15
00:00:53,390 --> 00:00:55,520
and we want to display
them in a different way.
16
00:00:55,520 --> 00:00:57,770
We want to do some operation.
17
00:00:57,770 --> 00:01:00,120
We can use a
function to do that.
18
00:01:00,120 --> 00:01:05,060
So the function is going to
change that data as we see it
19
00:01:05,060 --> 00:01:07,010
in whatever way we specify.
20
00:01:07,010 --> 00:01:09,380
So we'll use a
function, for instance,
21
00:01:09,380 --> 00:01:14,150
to show the lower case
values of the ename column.
22
00:01:17,910 --> 00:01:20,500
So notice what
we're seeing here.
23
00:01:20,500 --> 00:01:23,450
This is, in essence,
simple data transformation.
24
00:01:23,450 --> 00:01:27,940
So we have the values stored
in uppercase in the database.
25
00:01:27,940 --> 00:01:31,690
But we're wanting to
see them in lowercase.
26
00:01:31,690 --> 00:01:33,590
And that's what a function does.
27
00:01:33,590 --> 00:01:36,670
So in this case, our
function is called lower.
28
00:01:36,670 --> 00:01:39,450
We always use parentheses.
29
00:01:39,450 --> 00:01:42,240
And then we surrounded
it with a column name.
30
00:01:42,240 --> 00:01:48,170
So go to each value of
the ename column, Smith,
31
00:01:48,170 --> 00:01:50,280
and change it to
lowercase, Allen,
32
00:01:50,280 --> 00:01:52,100
and change it to lowercase.
33
00:01:52,100 --> 00:01:54,710
So a single row function
goes and operates
34
00:01:54,710 --> 00:01:58,070
on each individual row.
35
00:01:58,070 --> 00:02:00,770
And there are a number
of different string type
36
00:02:00,770 --> 00:02:02,670
functions like this.
37
00:02:02,670 --> 00:02:04,530
So we're going to look
at a couple of them.
38
00:02:04,530 --> 00:02:05,900
There are some
that we won't take
39
00:02:05,900 --> 00:02:09,230
a look at because they're
less common or a little more
40
00:02:09,230 --> 00:02:10,280
obscure.
41
00:02:10,280 --> 00:02:13,490
The idea here is to get
how a function works
42
00:02:13,490 --> 00:02:16,820
and what it can do for
your data processing.
43
00:02:16,820 --> 00:02:23,870
So let's change to a different
function called a initcap
44
00:02:23,870 --> 00:02:26,610
and execute.
45
00:02:26,610 --> 00:02:28,850
So here, we're using mixed case.
46
00:02:28,850 --> 00:02:32,270
So are initcap function
projects this data
47
00:02:32,270 --> 00:02:34,940
and transforms it
into mixed case.
48
00:02:34,940 --> 00:02:38,660
So we might like to see the
data be in mixed case instead
49
00:02:38,660 --> 00:02:41,870
of all capitals if we're
running a report generating
50
00:02:41,870 --> 00:02:44,210
labels or something like that.
51
00:02:44,210 --> 00:02:48,830
So the initcap function will
do that operation on each row.
52
00:02:48,830 --> 00:02:52,790
That's what a single
row function does.
53
00:02:52,790 --> 00:02:54,720
Let's take a look at
the length function.
54
00:02:57,880 --> 00:03:00,030
So here, our function is length.
55
00:03:00,030 --> 00:03:05,220
And we're using the
ename column, execute.
56
00:03:05,220 --> 00:03:09,360
And I'll add ename in here
just to make this more visually
57
00:03:09,360 --> 00:03:11,700
obvious.
58
00:03:11,700 --> 00:03:16,130
So what length is going to do,
it's going to look at a string,
59
00:03:16,130 --> 00:03:19,640
and then present the
number of characters
60
00:03:19,640 --> 00:03:21,750
in each one of the values.
61
00:03:21,750 --> 00:03:25,190
So for instance, Smith
has a length of five,
62
00:03:25,190 --> 00:03:29,330
Ward has a length of four,
Martin a length of six,
63
00:03:29,330 --> 00:03:31,230
and so on and so forth.
64
00:03:31,230 --> 00:03:34,130
So the length function is
usually used in conjunction
65
00:03:34,130 --> 00:03:36,260
with other functions
that we'll look at
66
00:03:36,260 --> 00:03:39,530
to give us that information
and maybe pass that information
67
00:03:39,530 --> 00:03:43,230
on to another function.
68
00:03:43,230 --> 00:03:46,520
Let's look at the substring
string manipulation
69
00:03:46,520 --> 00:03:48,740
function, ename.
70
00:03:53,240 --> 00:03:59,060
And again for clarity, I'll
show the before and after.
71
00:03:59,060 --> 00:04:02,650
So the substring is
going to take and look
72
00:04:02,650 --> 00:04:05,350
at a string value, such
as any of these string
73
00:04:05,350 --> 00:04:08,560
values, these character values
and in the ename column.
74
00:04:08,560 --> 00:04:13,790
And then it's going to return
a portion of that string.
75
00:04:13,790 --> 00:04:19,090
So here, we're seeing the first
three characters in each name.
76
00:04:19,090 --> 00:04:21,880
And that's because we've
used the substring function,
77
00:04:21,880 --> 00:04:24,730
but we're passing it three
values in this case instead
78
00:04:24,730 --> 00:04:26,140
of just the column.
79
00:04:26,140 --> 00:04:28,000
So we give it the column.
80
00:04:28,000 --> 00:04:31,810
The next value that we pass
is the starting character, so
81
00:04:31,810 --> 00:04:33,580
the first character.
82
00:04:33,580 --> 00:04:37,270
And the third value that
we're passing into substring
83
00:04:37,270 --> 00:04:41,120
is the total number of
characters to be returned.
84
00:04:41,120 --> 00:04:45,250
So start at the first character
and return the first three
85
00:04:45,250 --> 00:04:47,220
characters.
86
00:04:47,220 --> 00:04:48,470
We can change this.
87
00:04:48,470 --> 00:04:52,490
Start at the second character
and return four characters,
88
00:04:52,490 --> 00:04:54,940
for instance.
89
00:04:54,940 --> 00:04:57,050
And notice that it
starts at the second
90
00:04:57,050 --> 00:04:59,360
and returns four characters.
91
00:04:59,360 --> 00:05:02,570
In the case of Ward, it
starts at the second character
92
00:05:02,570 --> 00:05:05,270
and doesn't have
four to present.
93
00:05:05,270 --> 00:05:07,700
And so it does the
three that it can.
94
00:05:07,700 --> 00:05:10,130
And that's just because Ward
is equal to four characters,
95
00:05:10,130 --> 00:05:11,510
and we're starting
at the second.
96
00:05:14,840 --> 00:05:17,930
Let's take a look at
the to_char function.
97
00:05:17,930 --> 00:05:20,680
This is a really
powerful function.
98
00:05:20,680 --> 00:05:23,740
It's a little more complex.
99
00:05:23,740 --> 00:05:29,290
To-char is going to be used
to take a different data type
100
00:05:29,290 --> 00:05:31,060
and present it as a character.
101
00:05:42,330 --> 00:05:45,010
And execute.
102
00:05:45,010 --> 00:05:47,390
All right, so what are
we looking at here?
103
00:05:47,390 --> 00:05:49,220
Well, let's start with
the hire date column.
104
00:05:49,220 --> 00:05:51,930
All right, so hire
data is stored
105
00:05:51,930 --> 00:05:56,640
in the database as a two digit
day, three digit month, and two
106
00:05:56,640 --> 00:05:58,770
digit year.
107
00:05:58,770 --> 00:06:02,360
But what if we want more
information than that?
108
00:06:02,360 --> 00:06:05,240
How do we extract
time information,
109
00:06:05,240 --> 00:06:07,550
change the formatting
of the data?
110
00:06:07,550 --> 00:06:12,920
What if we wanted it to say
December 17th, 1980, instead
111
00:06:12,920 --> 00:06:14,990
of 17, December?
112
00:06:14,990 --> 00:06:17,750
That's what the to_char
function is used to do.
113
00:06:17,750 --> 00:06:22,040
It's often used to manipulate
dates, probably most often,
114
00:06:22,040 --> 00:06:24,690
to a format that
we want to display.
115
00:06:24,690 --> 00:06:28,760
So what we've got here is
what's called a format mask.
116
00:06:28,760 --> 00:06:30,900
So what does all this mean?
117
00:06:30,900 --> 00:06:33,350
Well, we start out
is a format mask
118
00:06:33,350 --> 00:06:35,210
is really giving us
a template for how
119
00:06:35,210 --> 00:06:37,380
we want to display the data.
120
00:06:37,380 --> 00:06:43,570
So the DD part of the format
mask means two digit day.
121
00:06:43,570 --> 00:06:47,600
MON means three digit
month abbreviation.
122
00:06:47,600 --> 00:06:51,560
YYYY means four digit year.
123
00:06:51,560 --> 00:06:53,980
Notice that differentiation.
124
00:06:53,980 --> 00:06:58,910
HH24 means hours on
the 24 hour clock.
125
00:06:58,910 --> 00:07:00,650
MI is minutes.
126
00:07:00,650 --> 00:07:03,690
SS is seconds from
the emp table.
127
00:07:03,690 --> 00:07:06,300
And so it's taking this
data and presenting it
128
00:07:06,300 --> 00:07:08,310
in a different way.
129
00:07:08,310 --> 00:07:11,730
So let's change the format
mask to be something
130
00:07:11,730 --> 00:07:13,610
that's even more visible.
131
00:07:19,870 --> 00:07:21,550
All right, so now
we're not extracting
132
00:07:21,550 --> 00:07:25,780
time information, but just
the date information itself.
133
00:07:25,780 --> 00:07:28,010
And we're showing it
in a different format.
134
00:07:28,010 --> 00:07:30,640
So now instead of
MON in capitals,
135
00:07:30,640 --> 00:07:33,400
we use mixed case
Month, and that's
136
00:07:33,400 --> 00:07:38,850
going to actually show
the month value itself.
137
00:07:38,850 --> 00:07:41,240
Let's turn to a different
type of function.
138
00:07:45,900 --> 00:07:49,160
And we're looking at
the round function.
139
00:07:49,160 --> 00:07:50,800
So at this point,
we don't see a lot
140
00:07:50,800 --> 00:07:53,620
of difference between these
values because of the way
141
00:07:53,620 --> 00:07:54,940
that we're rounding them.
142
00:07:54,940 --> 00:07:59,020
We're really passing in a
default value for the rounding
143
00:07:59,020 --> 00:08:03,920
location, which is just to
the nearest one's value.
144
00:08:03,920 --> 00:08:09,200
Let's try something like this.
145
00:08:09,200 --> 00:08:14,040
So notice here that we
have a SAL value of 1250.
146
00:08:14,040 --> 00:08:17,010
But in the rounded
column, we have 1300.
147
00:08:17,010 --> 00:08:20,680
So 1250 has been
rounded up to 1300.
148
00:08:20,680 --> 00:08:23,830
The round function is going to
take a value, such as the SAL
149
00:08:23,830 --> 00:08:27,010
column, and round it to
the place that we specify.
150
00:08:27,010 --> 00:08:30,490
So here, we're specifying
to the negative two place.
151
00:08:30,490 --> 00:08:34,520
So it's going to go back
two from the decimal point
152
00:08:34,520 --> 00:08:36,250
and round to that spot.
153
00:08:36,250 --> 00:08:39,730
So a 1250 is going
to round to 1300.
154
00:08:39,730 --> 00:08:42,580
For instance, 2975
is going to round
155
00:08:42,580 --> 00:08:45,050
to 3000, so on and so forth.
156
00:08:45,050 --> 00:08:48,490
So the round function
is used to round values.
157
00:08:48,490 --> 00:08:52,270
Now normally what you would
round are decimal values.
158
00:08:52,270 --> 00:08:55,600
In that case, we would
use non-negative numbers
159
00:08:55,600 --> 00:08:58,300
to round to the
second decimal point
160
00:08:58,300 --> 00:09:01,590
to the right of the
decimal, for instance.
12742
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.