DateTime functions in SQL Server Part 25 — Transcript
Full transcript
- 0:00hello welcome to preim Technologies I'm
- 0:02wenet this is part 25 of SQL server in
- 0:05this session we'll learn about datetime
- 0:08data types date time functions and
- 0:11understand the terms UTC time and time
- 0:13zone
- 0:14offset in SQL Server there are several
- 0:17data types as far as date time is
- 0:19concerned time date small date time Etc
- 0:24now depending on the requirement of the
- 0:26application we choose the appropriate
- 0:28date time data type for example if I
- 0:32want to store only the date of birth of
- 0:35a person then it's enough if I choose
- 0:37the date data type on the other hand
- 0:40instead of choosing date data type if
- 0:42you choose date time you know let's say
- 0:45I want to store only the date of birth I
- 0:48don't want to capture the time but
- 0:50instead of choosing date data type I've
- 0:53chosen data type date time data type
- 0:56what is the disadvantage of doing so if
- 0:58you look at datetime data type it has
- 1:00got eight bytes the size but whereas
- 1:03date data type is only three bytes so
- 1:06when it's not required to capture the
- 1:08time then probably it's you know it
- 1:11makes sense to choose the date data type
- 1:14alone rather than choosing date time
- 1:16data type so on the other hand let's say
- 1:19I want to store the time as well maybe
- 1:21that's when we choose the date time data
- 1:24type okay maybe I want to store only the
- 1:26time in those cases maybe I'll choose
- 1:28the time data type
- 1:30okay or if I want more Precision for the
- 1:33Nan seconds then I choose dat time to
- 1:37data type so here year month date hours
- 1:41milliseconds I mean minutes seconds and
- 1:45Nano second so there is more Precision
- 1:48for nanocs here in date time too when we
- 1:51compare that with date time okay now we
- 1:54have this daytime offset let us
- 1:57understand what we mean by daytime
- 1:59offset now if if you look at the date
- 2:00time offset it's pretty much similar to
- 2:03date time two okay we have years month
- 2:08days and then hours minutes seconds Nan
- 2:11seconds and then you have CAU some plus
- 2:13you know optionally plus or minus symbol
- 2:16and then you've got hours and minutes
- 2:18okay so this piece here the plus or
- 2:22minus hours and minutes this is called
- 2:24the time zone
- 2:27offset and this time you know when you
- 2:30use date time offset data type this time
- 2:33is usually you know in UTC time okay
- 2:39this Falls in UTC time zone now let us
- 2:42understand what exactly do we mean by
- 2:44UTC time and you know the time zone
- 2:49offset
- 2:51okay so UTC basically stands for
- 2:54coordinated universal time now here is
- 2:57another confusion UTC but if if you look
- 3:00at coordinated universal time it is
- 3:03C so how is this an expanded form of
- 3:06this abbreviation now there are some
- 3:08political reasons behind why they have
- 3:10actually abbreviated it like this but
- 3:12actually this stands for coordinated
- 3:14universal time now we'll understand the
- 3:16concepts behind what do we mean by
- 3:19coordinated universal time and uh the
- 3:22time zone offset rather than worrying
- 3:24about the political reasons behind
- 3:26abbreviating it like that so UTC stands
- 3:29for coordin ated universal time so
- 3:32basically this UTC is is the you know is
- 3:38the central Time based on which the
- 3:40world regulates clocks and time okay now
- 3:45UTC and there is another time zone
- 3:47called GMT green wi meantime okay
- 3:50they're actually synonymous with each
- 3:53other for all common purposes but
- 3:56however there are very slight
- 3:57differences you know uh but most of the
- 4:00time gmta and UTC are are synonymous for
- 4:04all common
- 4:06purposes okay so what do we mean by the
- 4:09cutc okay to understand this practically
- 4:12you know all of us have worked with the
- 4:14clock and Windows operating system so if
- 4:16I open the clock in Windows operating
- 4:18system let's say I want to change the
- 4:21date and time settings I click that and
- 4:23when we go to change time zone so if you
- 4:26look at this time zone here and if I
- 4:29draw this list down you look at this
- 4:32these are all the time zones if not all
- 4:35most of the time zones in the world okay
- 4:38now if you look at this there is
- 4:41something called UTC here which is
- 4:45nothing but coordinated universal time
- 4:48okay and then if you look some of these
- 4:50here so UTC plus 2 hours UTC plus 3
- 4:55hours okay so this UTC is the Cod
- 4:59coordinated universal time the central I
- 5:02can say the world Standard Time based on
- 5:05which other clocks and times in
- 5:08different time zones are
- 5:10calculated okay so if in UTC the time is
- 5:1412:00 at the minute then if we want to
- 5:18calculate what is the time in Germany
- 5:21which is Berlin so if it is 12:00 in UTC
- 5:24coordinated universal time in Germany
- 5:27it's 12 hours I mean 12 in the noon + 1
- 5:31which means 13 hours 1:00 in the
- 5:34afternoon in Germany okay on the other
- 5:36hand if we want to find out what's the
- 5:38time in New Delhi so New Delhi is around
- 5:425 and 1 half hours from UTC which is
- 5:45India so the time in I mean in UTC time
- 5:49zone if it's 12:00 uh then it's 5:30 in
- 5:54the evening in
- 5:55India okay so all the other time
- 6:00zones are based on UTC so so that plus
- 6:05or minus hours so if you have to look at
- 6:08the countries on the negative side I
- 6:10mean on the minus side UTC minus
- 6:13something look at this um
- 6:16Central Central America is UTC minus 6
- 6:21hours so this minus plus or minus thing
- 6:24is nothing but the offset from the um
- 6:28UTC time
- 6:31zone okay now I'm right now in London so
- 6:34if you look at the time right now in
- 6:36London it is
- 6:372218 now let me try to switch my time
- 6:40zone to UTC and see what happens so when
- 6:44I select UTC coordinated universal time
- 6:48look at this uh current date and time on
- 6:51my machine is 228 which is 10:00 in the
- 6:55night okay but UTC time is is
- 7:002119 which means London's time is
- 7:03actually 1 hour ahead of the UTC time
- 7:07okay but if you look at the London time
- 7:09zone here we don't have that plus 1 hour
- 7:13extra here okay it just says okay UTC
- 7:17coordinated universal time and then UTC
- 7:20uh Dublin edenburg Lisbon London okay
- 7:23but then if you look at the time that I
- 7:25have selected there is 1 hour difference
- 7:27okay there was 1 hour difference
- 7:32so if I select London let's cancel that
- 7:35let's go back change date and time
- 7:38change time zone look at this it's
- 7:412219 now if I select UTC time so there's
- 7:461 hour difference between UTC and London
- 7:50but then it doesn't show up in that
- 7:51drop- down list so why is
- 7:55that that's because there is another
- 7:57concept called daylight s savings okay
- 8:01so since this is summer here here um we
- 8:04actually have moved our clocks you know
- 8:081
- 8:09hour you know 1 hour forward because of
- 8:13which we are getting that difference
- 8:15between uh C I mean the UTC time and the
- 8:19time in London otherwise you know in
- 8:22winter again we'll move it backwards and
- 8:24UTC time matches with London's time so
- 8:27that's why daylight savings are not
- 8:28taken into conation here that's the
- 8:30reason why you see uh that difference
- 8:33when I change the data date
- 8:35here okay so I I hope it's clear what is
- 8:39UTC time and time
- 8:42zone
- 8:44okay all right so in order to understand
- 8:48these different data types I've created
- 8:49a very simple table here um called tbll
- 8:53date time and I have used all the
- 8:56datetime data types that are available
- 8:58you know time date small dat time dat
- 9:00time Etc in this table just to show you
- 9:03practically how the data get stored in
- 9:05the table so if you look at this we have
- 9:09this column C time which is time data
- 9:12type c dat is just date data type small
- 9:16date time is small date time I have
- 9:18include ccore just to you know give it a
- 9:22you know ccore indicates column in that
- 9:25table it's about that so when I select
- 9:28the all the rows from this table we
- 9:29don't have any data in those columns
- 9:32okay let's try and insert some data into
- 9:35these columns okay now look at this in
- 9:37order to get a system date and time the
- 9:41current date and time in SQL Server I
- 9:43can use a function called get date we
- 9:46have spoken about string many string
- 9:48functions for example substring lower
- 9:51upper just like all these functions to
- 9:53work with datetime data types we have
- 9:56datetime functions and one of the very
- 9:58common functions that we use is get date
- 10:00which will basically give us the current
- 10:02system date and time so when I execute
- 10:04that I get the current system date and
- 10:06time now I'm using this function instead
- 10:08of typing in the values for time date
- 10:12small date time you know for all these
- 10:14columns I am actually using this method
- 10:18okay so it's going to return if you look
- 10:20at this select get date it's going to
- 10:23return the current system date and time
- 10:26okay year month days hour minutes
- 10:30seconds and milliseconds I mean nanocs
- 10:32rather okay so now when I try to execute
- 10:36this
- 10:38query so one row affected which means
- 10:40one row got inserted into this table
- 10:43called TBL day time so if I select this
- 10:46look at this time is only storing the
- 10:50time hours minutes seconds and the Nan
- 10:53seconds so which is nothing but our time
- 10:56data type hours minutes seconds and Nan
- 10:58seconds and along the same lines if you
- 11:00look at daytime
- 11:01offet so it's storing year month days
- 11:06hours minutes seconds Nan seconds and
- 11:10the time zone offset now since get
- 11:13date if you see the time zone offset
- 11:16difference it's only 0 0 here that's
- 11:18because when I say get date it doesn't
- 11:21give any value for time zone offset so
- 11:24that's why you see the time zone offset
- 11:26difference is plus 0 0 on the other hand
- 11:29if you want to update that you can
- 11:31update with any time zone offset but if
- 11:34you want to store the time zone offset
- 11:36in daytime to data type or datetime data
- 11:39type it's not practically possible it
- 11:41doesn't allow you to do
- 11:43that okay so let's quickly update it
- 11:47with some
- 11:48offset so I want to say the offset is
- 11:52maybe plus one hour for this time I've
- 11:56just written the update query beforehand
- 11:58you know in order to save time in typing
- 12:01so when I press F5 you should see we got
- 12:04an offset there and know it's 10 hours
- 12:08okay all right
- 12:13cool okay so these are some of the
- 12:15functions that we can use in SQL Server
- 12:18to get the current system date and time
- 12:22in SQL Server you know we have just seen
- 12:24how to use geted function to return the
- 12:27current system date and time okay on my
- 12:30computer I have SQL Server installed so
- 12:33on the computer where you have SQL
- 12:34Server installed if you want to get the
- 12:36time and date from that machine then you
- 12:39can use select get date okay let's say
- 12:41if I am connected to SQL server using a
- 12:43SQL Server management Studio from a
- 12:45different computer and when I connect to
- 12:48that SQL Server from SQL Server
- 12:49management Studio installed on that
- 12:52client computer and when I execute
- 12:53select get date I get the date and time
- 12:57of the SE of where you have the SQL
- 13:00Server installed rather than on your
- 13:02computer from which you have connected
- 13:05to SQL
- 13:06Server okay so apart from get date you
- 13:09can also use various other functions
- 13:11like current time stamp system date time
- 13:13system date time offset get UTC date and
- 13:16sis UTC date time so if you look at
- 13:20these functions they written the current
- 13:22date and time in different formats okay
- 13:25so for example we have just spoken about
- 13:28what is UTC time and the time zone
- 13:31offset so if you want you know maybe um
- 13:36the time zone offset as well you can use
- 13:39system s date time offset that will give
- 13:42you the time in UTC I mean date time in
- 13:45UTC and then the offset the
- 13:49difference okay so if you look
- 13:52at
- 13:54says says date time offset so I've got
- 13:57the time and UT TC and 1 hour is the
- 14:01difference now right now in London the
- 14:03time is
- 14:062226 okay which is 1 hour
- 14:11ahead of the UTC
- 14:17time okay so along the same lines you
- 14:20have these other functions so depending
- 14:21on on your requirement how you want to
- 14:24retrieve the date and time you choose
- 14:27that function so and out of these
- 14:30functions get dat is very commonly used
- 14:32if you look at current timestamp and get
- 14:34dat they written exactly the same date
- 14:36and time format the only difference
- 14:38between them is current time stamp is an
- 14:41an SQL equivalent to get date okay
- 14:44whereas CIS date time gives us more
- 14:47fractional seconds
- 14:49Precision okay CIS date time offset
- 14:52which we have just say you know you have
- 14:54more fractional seconds Precision plus
- 14:57the time zone offset
- 14:59and if you want the UTC date and time
- 15:01you use get UTC date time and along the
- 15:05same lines sis UTC date
- 15:08time okay which has more fractional
- 15:11seconds
- 15:13Precision along with UTC date
- 15:16time on this slide you can find
- 15:18resources for asp.net and C interview
- 15:21questions that's it for today thank you
- 15:23for listening have a great day
About this transcript
This page contains the full transcript of DateTime functions in SQL Server Part 25 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,309 words across 324 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.