📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 10

2022-05-05 •Youtube

Detailed Notes

1. Date and time functions select adddate(type_datetime, INTERVAL 1 DAY), adddate(type_date, INTERVAL 1 DAY), adddate(type_year, INTERVAL 1 DAY) from all_dates; select adddate(type_datetime, INTERVAL 1 MONTH), adddate(type_date, INTERVAL 1 MONTH), adddate(type_year, INTERVAL 1 MONTH) from all_dates;

select adddate(type_datetime, INTERVAL -2 MONTH), adddate(type_date, INTERVAL -2 MONTH), adddate(type_year, INTERVAL -2 MONTH) from all_dates;

select adddate(type_datetime, INTERVAL 1 YEAR), adddate(type_date, INTERVAL 1 YEAR), adddate(type_year, INTERVAL 1 YEAR) from all_dates;

select year(adddate(type_datetime, INTERVAL 1 YEAR)) dtm, year(adddate(type_date, INTERVAL 1 YEAR)) dt, year(adddate(type_year, INTERVAL 1 YEAR)) yr from all_dates;

select dayname(type_datetime), dayname(type_date) from all_dates;

select adddate(type_datetime, INTERVAL 5 SECOND), adddate(type_date, INTERVAL 5 SECOND) from all_dates;

select datediff(type_datetime,now(quit)), type_datetime, now() from all_dates; select datediff(type_datetime,now()) / 365, type_datetime, now() from all_dates;

Transcript Text
[Music]
hello and welcome back we are continuing
our
sql uh tutorial and examples
and we're moving right along focusing on
of course my sequel and maria db
and today we're going to continue
sort of from the last episode where we
talked about dates and times and date
times and time stamps
and we're just going to touch on
some of the functions that are built in
to
my sql so if we just start from what we
call it all dates i'm going to show
these real quick
so
i should probably say from
okay so we've got our little date table
oh and to finish that thought we're
going to touch on this because there are
dozens
of
functions around date stuff
you can google them if you google like
my sql date functions you'll see should
be able to see some pretty good examples
of them
and you can see the documentation and
see what all is in there
but i do want to give you a
a feel for
what is there
so you're not you know banging your head
against the wall trying to get things
trying to do something that's already
been done for you
so the first one i want to do is let's
do this here select star from
all dates except for let's do
uh we're gonna do the date time first
well actually let's do this what we're
going to do is date time
and we're also going to use typed
actually we're going to use all of these
let's do this
because it may be easiest to see what
happens with each of these so first one
let's start with um
the arithmetic
we talked about that last time and so
there's a
there's a thing called add date or add
time
which is no underscore and it's just
going to be add date
like that
and let's put him here
and i want to show you what happens in
each of these cases
and the format for this
the format for this
is that we have an interval
and let's do
let's start with just one day
and you're going to see that we can
actually this interval can be a whole
lot of interesting things
so first let's do this and see what it
tells us
so we're adding one day to each of these
now adding a date to a year is just
going to return back
a
a null
adding a day to a regular date we were
this is the type date so 421 one two one
two now it's 422 one three one three
and the date time was four twenty one
one and we'd see four twenty one one two
now
we can do the same thing and we can
change it so let's instead of a day
that's at a month
so let's do that
we still get anything on our year
but we now see our months of being
incremented we can do the same thing
let's do it this way it's pretty easier
this way and let's go ahead and subtract
two months
uh there we go
so
it went from like five to
ah
yeah i must have missed that
somewhere i added an extra one somewhere
okay
um
but now notice that like here where we
subtract two we went from
uh what is it here let me do that one
more time
oh that's right i'm sorry i was thinking
it was updating but this is i'm actually
doing the select i'm not actually doing
an update i confused myself
so we started with
let's do that
so we started at 4
20
uh and the type date time
uh type date i'm sorry so we had four
so it's basically april january january
when we subtract two notice that we
jump back to february but here it even
does our arithmetic so it says that hey
we went to a prior year
and uh it does that with these as well
again type year hasn't been touched now
let's do but
oh i'm sorry
and that was on the
subtract so there's our subtract now
so now let's take
we've done a day
let's add a year
and so now we still don't see in the
year because it's not it doesn't really
treat it i guess properly as a date type
in a sense
uh what we do see is
uh here so we've got our
we've incremented remember these were 2
000.
um so we increment our years let's go
back up here so we can see them
uh
date time was 22 2000 2000
and date time is now 23 2001 2001
same thing on our type date year
note we're still not getting it here is
effectively
a
um
more like a number although it's a
special you know in a sense
it's still it's basically a number so
you're not going to see that
as useful as some of the other dates
which brings us to uh which we've
already seen
what i can do
is i can take the same thing
and i can actually cast stuff
so if i take just the year
out of each of these and we're going to
see the same thing i could take a year i
could take i could do a year i could do
month i could date i think we saw like
we can do before even that we can do
hour we can do
minute
and even second
so now let's just take that and see what
happens
well
i need to paste that
and now the year
of
uh oh this is
sorry it got too big there because of
that
um let me cast this so let's just do
uh
that's going to be dt
that's going to be just oh
that's going to be ttm dt
and year
let's do this way this is a nice way
also so we can shrink that thing
now we've got a nice small thing
okay
and we can see that for the date time
we got just a year date so we don't
really you know years they have their
uses but
uh and particularly because they're
smaller
but typically you may just you're
probably better off using a daytime or
you know a date
uh let's see so time
let's see what else we want to look at
now we can also do a date sub so we can
we can do
and actually there's a there's date
there's date add there's also
add date
i'm sorry
date add
and he's going to run the same way
um
so there's a couple of
of aliases basically of methods
or i guess functions i should say
now you can do
uh let's see let's go back up to
let's just start this one
we can also do
things like this we could do a day
of
year let's do this which you can also do
day of week day of month um
while i type
so i can do day of year for each of
these
which is a nice way to
quickly grab like particularly with the
year i mean i guess week awesome also
happens quite a bit where you're like
hey i want to know if it's the and we'll
show that in a second if it's a monday
sunday tuesday
whatever it is day of the week
so day of year i'm just going to take
type year out because it's not going to
like that
um so we'll show you that yeah see it's
still null but here we can see the day
of the year
um so april what was that april 21st is
obviously or apparently the 110th day of
the year and this depends on um leap day
or i'm sorry leap years
so depending on the year
you may have a slightly different
day of the year obviously
you can also do which is fairly useful
for us on a regular basis i want to show
you that one
i can do day of week
and i can see which day of the week each
of these are
and that's not always useful to see you
know which day the week is at and what i
can do is i can do
obtain it
if i can type it right day name
so i can take that
and now it doesn't like that
let's do oh probably because it's it
doesn't do it off of that it has to do
it off of this so instead of day of week
i can do day name
these are easy to forget there we go so
now we see wednesday saturdays sunday
and thursday
these are easy to forget because there's
a lot of
a lot of little variations of this and
if you're in other databases
oracle sql server
db2 whatever
they're going to be most likely very
similar type functions
they may work a little differently and
so
those are one things you're just gonna
have to worry about is you're gonna have
to figure out what is it that
i'm
i need to use in this database what's
the format you know which parameter
comes in which order
uh now the interval thing is a little
different a lot of times it's gonna be
like a you know plus one minus one or
stuff like that and then it may say you
know a type or something like that so
that is where it's gonna be different uh
you're not gonna see stuff that looks
like this uh you know add date interval
one
day kind of stuff
and that does work for year month day
hour minute second so i can do
i can do this same thing and i can add a
second i can add five seconds let's do
this
and i think
i can even put an s at the end i'm gonna
find out in a second
no pun intended
so
let's see let's do it this way right
first so i got that now let's see i
think it will allow me to put an s there
oh no it doesn't okay i was thinking it
might but it doesn't so
it's always going to be singular but the
interval
the number and then the type and so i
could do second i can do minute i can do
hour
so we do a lot of that
fairly straightforward fairly quickly we
also saw our casting so we can cast a
year to month today
um oh that's what i want to do is
let's do
uh
let's see
i don't want to do this okay
i'm going to do oops let's do this i'm
going to take these
and now i'm going to use
if we want to compare some we can always
do less than greater than
um
equals 2 not equal to which we've
already seen
but i can also do
i can compare two dates so let me do
that too
now and then to now
and here's the difference and it's going
to be in
seconds
um
so and we can see that
uh
now
to
uh
and it's let's see how to do it it's
basically the first one minus the second
one
is how you do it so if i flip those
what's the difference between
so to go from
if i want to know you know if i want to
go from type date time that value to now
i have to add that back in
and so this is
this this first part is uh 800 8146
seconds less than this and if i run it
again because it's now we're going to
see oh no it is about the same thing
so interesting
oh that may be in minutes my mistake
let's look at it this way
first
ah now we'll leave it like that and i
want to take that let's say
times 60.
oh
i'm sorry divided by 60.
uh
let's do divided by 3 600. uh well let's
see how many minutes let's do it that
way
so the difference here is
135 minutes
um let's look at it this way this is
going to be easier
i also want to do
um
let's do it this way
it's going to be easier to see
type
date time comma now
let's do this that way we can actually
look at what the value are so if we look
at the difference
uh over 60.
so from
uh and this is in the date diff
this is important is that for date diff
only
the date is used and this is the number
of days this is a the number of days uh
returned so we're going to see here
that 135 days is difference between
these two
uh
since this is a partial it's basically
oh because i did the i'm sorry
that's in days um
oh wait no we can't see that because if
i do it's not in days
that's day so let's do divided by
365.
let's do that
sorry guys
um i'm getting my getting confused on
these okay so here we go
uh it's
so the difference in these and it does
take this into account is that or i'm
sorry it doesn't take this into account
so this is going to be one regardless
which i'll show you in a minute but this
is basically 22 years
difference uh let me go show you that so
the date difference
even though the times are pretty close
um here it's just set 421 to 421 even
though you know those are about there so
even if i were to change that time
i'm still going to get this is the
difference in days
now there are others so if you want to
do time diff
uh if you want to do a timestamp
comparison
you can get those as well
so there's quite a bit of did i get that
in there
yep i got that
let's do this and let's do it by that
just so we've got it
so that's how many years
and
i think that's a good place to stop for
now so we're going to go ahead and wrap
this one up
and we are going to dive into whatever
our next topic happens to be um we've
got plenty we'll just see where it takes
us
but until then go out there and have
yourself a great day a great week and we
will talk to you next time
you
Transcript Segments
0.43

[Music]

27.199

hello and welcome back we are continuing

29.519

our

30.4

sql uh tutorial and examples

34

and we're moving right along focusing on

35.92

of course my sequel and maria db

38.96

and today we're going to continue

40.879

sort of from the last episode where we

42.48

talked about dates and times and date

45.039

times and time stamps

46.96

and we're just going to touch on

49.6

some of the functions that are built in

52.16

to

53.199

my sql so if we just start from what we

56.079

call it all dates i'm going to show

57.76

these real quick

58.96

so

60.8

i should probably say from

64.4

okay so we've got our little date table

67.6

oh and to finish that thought we're

69.119

going to touch on this because there are

71.52

dozens

72.4

of

73.28

functions around date stuff

75.68

you can google them if you google like

77.759

my sql date functions you'll see should

79.92

be able to see some pretty good examples

81.6

of them

82.64

and you can see the documentation and

84

see what all is in there

86.56

but i do want to give you a

88.88

a feel for

90.64

what is there

92.159

so you're not you know banging your head

94.4

against the wall trying to get things

96.56

trying to do something that's already

97.68

been done for you

100.32

so the first one i want to do is let's

102.64

do this here select star from

106.64

all dates except for let's do

110

uh we're gonna do the date time first

114.399

well actually let's do this what we're

115.68

going to do is date time

117.439

and we're also going to use typed

120.079

actually we're going to use all of these

121.84

let's do this

123.52

because it may be easiest to see what

126

happens with each of these so first one

129.2

let's start with um

131.92

the arithmetic

134.72

we talked about that last time and so

136.239

there's a

137.28

there's a thing called add date or add

139.04

time

140.56

which is no underscore and it's just

142.72

going to be add date

145.36

like that

149.44

and let's put him here

153.44

and i want to show you what happens in

155.28

each of these cases

158.72

and the format for this

160.319

the format for this

162.16

is that we have an interval

166.8

and let's do

169.84

let's start with just one day

172.959

and you're going to see that we can

173.92

actually this interval can be a whole

176.16

lot of interesting things

179.599

so first let's do this and see what it

182.08

tells us

186.319

so we're adding one day to each of these

189.2

now adding a date to a year is just

191.44

going to return back

193.68

a

194.4

a null

195.68

adding a day to a regular date we were

200.159

this is the type date so 421 one two one

203.76

two now it's 422 one three one three

206.879

and the date time was four twenty one

209.36

one and we'd see four twenty one one two

211.76

now

213.84

we can do the same thing and we can

216.08

change it so let's instead of a day

219.68

that's at a month

232.959

so let's do that

235.599

we still get anything on our year

237.68

but we now see our months of being

239.519

incremented we can do the same thing

241.92

let's do it this way it's pretty easier

243.519

this way and let's go ahead and subtract

249.68

two months

265.28

uh there we go

267.04

so

268.72

it went from like five to

274.88

ah

279.84

yeah i must have missed that

281.44

somewhere i added an extra one somewhere

283.36

okay

285.28

um

286.72

but now notice that like here where we

288.4

subtract two we went from

290.4

uh what is it here let me do that one

292.32

more time

298.08

oh that's right i'm sorry i was thinking

299.6

it was updating but this is i'm actually

300.96

doing the select i'm not actually doing

302.24

an update i confused myself

305.44

so we started with

312.56

let's do that

313.759

so we started at 4

316.4

20

317.52

uh and the type date time

319.68

uh type date i'm sorry so we had four

322.16

so it's basically april january january

323.84

when we subtract two notice that we

326.24

jump back to february but here it even

328.56

does our arithmetic so it says that hey

330.32

we went to a prior year

332.72

and uh it does that with these as well

335.52

again type year hasn't been touched now

337.919

let's do but

340.08

oh i'm sorry

341.36

and that was on the

342.88

subtract so there's our subtract now

345.36

so now let's take

347.12

we've done a day

349.919

let's add a year

366

and so now we still don't see in the

368

year because it's not it doesn't really

369.759

treat it i guess properly as a date type

371.759

in a sense

373.199

uh what we do see is

378.4

uh here so we've got our

380.639

we've incremented remember these were 2

382.4

000.

383.28

um so we increment our years let's go

386.16

back up here so we can see them

388.56

uh

389.68

date time was 22 2000 2000

392.639

and date time is now 23 2001 2001

396.8

same thing on our type date year

399.36

note we're still not getting it here is

401.52

effectively

403.44

a

405.44

um

406.56

more like a number although it's a

408.639

special you know in a sense

410.72

it's still it's basically a number so

412.72

you're not going to see that

414.8

as useful as some of the other dates

418.24

which brings us to uh which we've

419.919

already seen

422.24

what i can do

423.759

is i can take the same thing

426.8

and i can actually cast stuff

430.319

so if i take just the year

433.919

out of each of these and we're going to

435.599

see the same thing i could take a year i

437.44

could take i could do a year i could do

439.12

month i could date i think we saw like

441.28

we can do before even that we can do

443.039

hour we can do

444.96

minute

446.8

and even second

448.479

so now let's just take that and see what

451.44

happens

453.36

well

454.4

i need to paste that

457.68

and now the year

459.36

of

460.72

uh oh this is

464.24

sorry it got too big there because of

466.319

that

467.28

um let me cast this so let's just do

471.52

uh

473.68

that's going to be dt

476.639

that's going to be just oh

479.919

that's going to be ttm dt

483.759

and year

485.68

let's do this way this is a nice way

487.44

also so we can shrink that thing

490.56

now we've got a nice small thing

492.479

okay

494.24

and we can see that for the date time

496.72

we got just a year date so we don't

498.8

really you know years they have their

500.639

uses but

502.479

uh and particularly because they're

503.599

smaller

504.639

but typically you may just you're

506.319

probably better off using a daytime or

509.28

you know a date

511.199

uh let's see so time

514.88

let's see what else we want to look at

516.8

now we can also do a date sub so we can

521.2

we can do

522.959

and actually there's a there's date

524.64

there's date add there's also

527.68

add date

532.32

i'm sorry

533.68

date add

538.16

and he's going to run the same way

540.08

um

541.279

so there's a couple of

543.12

of aliases basically of methods

546.08

or i guess functions i should say

549.68

now you can do

551.44

uh let's see let's go back up to

556

let's just start this one

559.2

we can also do

560.88

things like this we could do a day

566.64

of

568.56

year let's do this which you can also do

571.279

day of week day of month um

576.24

while i type

580.72

so i can do day of year for each of

582.959

these

586.48

which is a nice way to

589.2

quickly grab like particularly with the

590.8

year i mean i guess week awesome also

593.2

happens quite a bit where you're like

594.32

hey i want to know if it's the and we'll

596.32

show that in a second if it's a monday

598.48

sunday tuesday

599.839

whatever it is day of the week

603.2

so day of year i'm just going to take

604.72

type year out because it's not going to

605.839

like that

607.76

um so we'll show you that yeah see it's

609.76

still null but here we can see the day

611.76

of the year

613.279

um so april what was that april 21st is

617.76

obviously or apparently the 110th day of

619.839

the year and this depends on um leap day

625.92

or i'm sorry leap years

627.92

so depending on the year

629.6

you may have a slightly different

632.48

day of the year obviously

634.399

you can also do which is fairly useful

637.279

for us on a regular basis i want to show

639.44

you that one

642.56

i can do day of week

652.959

and i can see which day of the week each

654.48

of these are

657.44

and that's not always useful to see you

659.6

know which day the week is at and what i

661.279

can do is i can do

668.72

obtain it

670.079

if i can type it right day name

683.92

so i can take that

686.88

and now it doesn't like that

693.92

let's do oh probably because it's it

696

doesn't do it off of that it has to do

697.6

it off of this so instead of day of week

700.399

i can do day name

706

these are easy to forget there we go so

707.6

now we see wednesday saturdays sunday

709.519

and thursday

711.36

these are easy to forget because there's

712.959

a lot of

715.04

a lot of little variations of this and

717.68

if you're in other databases

720.399

oracle sql server

722.24

db2 whatever

724.079

they're going to be most likely very

726.24

similar type functions

728.639

they may work a little differently and

730.32

so

731.6

those are one things you're just gonna

732.56

have to worry about is you're gonna have

733.839

to figure out what is it that

736.8

i'm

737.76

i need to use in this database what's

739.839

the format you know which parameter

741.44

comes in which order

743.12

uh now the interval thing is a little

746.16

different a lot of times it's gonna be

747.44

like a you know plus one minus one or

749.12

stuff like that and then it may say you

750.8

know a type or something like that so

752.959

that is where it's gonna be different uh

755.44

you're not gonna see stuff that looks

756.959

like this uh you know add date interval

759.92

one

760.72

day kind of stuff

762.399

and that does work for year month day

764.959

hour minute second so i can do

768.72

i can do this same thing and i can add a

770.959

second i can add five seconds let's do

773.2

this

780.24

and i think

781.68

i can even put an s at the end i'm gonna

783.6

find out in a second

785.68

no pun intended

789.44

so

790.8

let's see let's do it this way right

792.399

first so i got that now let's see i

795.04

think it will allow me to put an s there

801.76

oh no it doesn't okay i was thinking it

803.68

might but it doesn't so

805.76

it's always going to be singular but the

807.92

interval

809.519

the number and then the type and so i

811.68

could do second i can do minute i can do

813.44

hour

814.959

so we do a lot of that

817.68

fairly straightforward fairly quickly we

819.839

also saw our casting so we can cast a

821.76

year to month today

824.079

um oh that's what i want to do is

832.399

let's do

833.68

uh

835.12

let's see

836.88

i don't want to do this okay

839.68

i'm going to do oops let's do this i'm

841.44

going to take these

846.88

and now i'm going to use

848.639

if we want to compare some we can always

850.32

do less than greater than

852.16

um

853.519

equals 2 not equal to which we've

855.12

already seen

861.04

but i can also do

864.48

i can compare two dates so let me do

867.12

that too

868.839

now and then to now

877.04

and here's the difference and it's going

878.8

to be in

880.079

seconds

882.32

um

883.519

so and we can see that

886.56

uh

888.88

now

890.88

to

891.76

uh

893.279

and it's let's see how to do it it's

895.6

basically the first one minus the second

897.68

one

898.72

is how you do it so if i flip those

900.56

what's the difference between

902.48

so to go from

905.12

if i want to know you know if i want to

906.72

go from type date time that value to now

911.6

i have to add that back in

914.48

and so this is

916.16

this this first part is uh 800 8146

920.32

seconds less than this and if i run it

922.959

again because it's now we're going to

924.24

see oh no it is about the same thing

927.68

so interesting

929.12

oh that may be in minutes my mistake

931.04

let's look at it this way

932.72

first

935.279

ah now we'll leave it like that and i

937.36

want to take that let's say

939.68

times 60.

946.24

oh

947.6

i'm sorry divided by 60.

957.44

uh

958.639

let's do divided by 3 600. uh well let's

961.519

see how many minutes let's do it that

963.04

way

965.36

so the difference here is

968.36

135 minutes

971.279

um let's look at it this way this is

973.6

going to be easier

975.12

i also want to do

978.56

um

981.04

let's do it this way

982.959

it's going to be easier to see

984.959

type

988.32

date time comma now

994.639

let's do this that way we can actually

996.32

look at what the value are so if we look

997.759

at the difference

998.959

uh over 60.

1001.36

so from

1003.759

uh and this is in the date diff

1008.32

this is important is that for date diff

1012.72

only

1014.32

the date is used and this is the number

1016.16

of days this is a the number of days uh

1019.12

returned so we're going to see here

1022.32

that 135 days is difference between

1025.199

these two

1026.24

uh

1027.439

since this is a partial it's basically

1030.48

oh because i did the i'm sorry

1032.559

that's in days um

1036.64

oh wait no we can't see that because if

1038.319

i do it's not in days

1042.319

that's day so let's do divided by

1044.959

365.

1046.799

let's do that

1048.559

sorry guys

1049.76

um i'm getting my getting confused on

1052.16

these okay so here we go

1053.84

uh it's

1056.64

so the difference in these and it does

1058

take this into account is that or i'm

1060

sorry it doesn't take this into account

1062.559

so this is going to be one regardless

1065.36

which i'll show you in a minute but this

1067.2

is basically 22 years

1069.36

difference uh let me go show you that so

1071.84

the date difference

1074

even though the times are pretty close

1077.76

um here it's just set 421 to 421 even

1081.2

though you know those are about there so

1082.72

even if i were to change that time

1085.28

i'm still going to get this is the

1086.48

difference in days

1088.96

now there are others so if you want to

1090.48

do time diff

1091.919

uh if you want to do a timestamp

1094.88

comparison

1097.039

you can get those as well

1098.96

so there's quite a bit of did i get that

1101.76

in there

1102.72

yep i got that

1106.4

let's do this and let's do it by that

1110.16

just so we've got it

1114

so that's how many years

1116.24

and

1118.16

i think that's a good place to stop for

1119.44

now so we're going to go ahead and wrap

1121.679

this one up

1123.039

and we are going to dive into whatever

1125.84

our next topic happens to be um we've

1128

got plenty we'll just see where it takes

1129.919

us

1130.72

but until then go out there and have

1132.08

yourself a great day a great week and we

1134.64

will talk to you next time

1153.36

you