DatePart, DateAdd and DateDiff functions in SQL Server Part 27

Поделиться
HTML-код
  • Опубликовано: 31 авг 2012
  • In this video we will learn about builtin date time system functions in sql server. DatePart, DateAdd and DateDiff functions in SQL Server will be discussed along with a real time example of using these functions.
    Text version of the video
    csharp-video-tutorials.blogspo...
    Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our RUclips channel. Hope you can help.
    / @aarvikitchen5572
    Slides
    csharp-video-tutorials.blogspo...
    All SQL Server Text Articles
    csharp-video-tutorials.blogspo...
    All SQL Server Slides
    csharp-video-tutorials.blogspo...
    All Dot Net and SQL Server Tutorials in English
    ruclips.net/user/kudvenka...
    All Dot Net and SQL Server Tutorials in Arabic
    / kudvenkatarabic

Комментарии • 115

  • @eniaonthestreets
    @eniaonthestreets 4 года назад +24

    I felt the need to comment because you really helped me with your tutorials!
    Thank you, Venkat, for all the effort you make to present programming in such an easy way, no matter how noob somebody may be.
    You are the best teacher I could find on youtube. Keep up the good work! Hope it pays back...

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  4 года назад +7

      Hello Maria - Thank you very much for the feedback. Means a lot. I have included all the sql tutorial videos, slides and text articles in sequence on the following page. Hope you will find it handy.
      www.pragimtech.com/courses/sql-server-tutorial-for-beginners/
      When you have some time, can you please leave your rating and valuable feedback on the reviews tab. It really helps us. Thank you. Good luck and all the very best with everything you are doing.

  • @bhagawatbhandari734
    @bhagawatbhandari734 8 лет назад +29

    Sir, you simply deserve heaven. Outstanding tutorial.

  • @johnceena9799
    @johnceena9799 3 года назад +6

    Your videos are so easy to understand that I completed all your videos in this playlist in 3 days by playing it at X1.5 speed.
    I only had 5 days before my job starts and i had no idea of SQL Server.
    Now, I am really confident to attend my job.
    Thank You Venkat from my bottom of my heart ❤️

  • @adenikeakinroluyo5883
    @adenikeakinroluyo5883 6 лет назад +3

    Kudvenkat, you're one of the best teachers I've ever known. Simple, step by step explanation of topics. Easy to understand.
    Thanks.

  • @rajatshrivastava879
    @rajatshrivastava879 4 года назад +2

    You are a gem. Explains Tricky concepts with such ease. Thanks for this complete video tutorial series.

  • @onlineaccount-wh2yx
    @onlineaccount-wh2yx 7 месяцев назад

    This may be little silly,but I love the calmness in your videos. Please don't change this . Extremely thankful for sharing your knowledge with us.

  • @bestherk4522
    @bestherk4522 3 года назад +1

    Years gone... But this video; in the first few minutes, I was able to spot what I was missing. Thank you. Really happy. #2020

  • @dzlee218
    @dzlee218 7 лет назад +1

    what's wrong with teh people that downvoted this video? not only this video, but the entire series is simply awesome

  • @Csharp-video-tutorialsBlogspot
    @Csharp-video-tutorialsBlogspot  11 лет назад +3

    Thank you very much for taking time to give feedback. I upload training videos on a daily basis. If you want receive email alerts when new videos are uploaded, please feel free to subscribe to my youtube channel.

  • @artokilponen6989
    @artokilponen6989 7 лет назад +6

    If you have problems understanding the DATEDIFF, keep in mind these four things:
    1) It returns as integer.
    2) It counts how many times you have to cross a line (from one year to another etc.)
    3) It doesn't care about greater accuracy than you pass in as the first parameter. Year 2017 is year 2017, no matter whether it is January first or December 31st. So between 1.1.2017 and 31.12.2017 you are in the same year, datediff in years is zero, because you are essentially passing as parameters XX.XX.2017 and XX.XX.2017. And from 31.12.2016 to 1.1.2018 you have to cross two lines (to 2017 and then to 2018), the datediff returns two.
    4) The +/- mark just tells whether you are going forward or backwards in time.
    At least for me it took a while to understand the function, but with these four points, I think it should become pretty simple.

    • @tusharghude15
      @tusharghude15 6 лет назад

      Arto Kilponen +1 for pointing out datediff counts when u cross the boundary for specified parameter

  • @sriharsha1549
    @sriharsha1549 9 лет назад +2

    Excellent. Probably the best tutorial. Thank you very much sir..

  • @Trzbne
    @Trzbne 6 лет назад +1

    Thank you very much, it was an excellent tutorial! Even the more difficult part was digested :-) I do not believe in copy-paste learning, I do not believe, that you can learn well, if you just copy the part that is too complicated for you. If you write it by yourself, you learn more.

  • @vadaliprasad
    @vadaliprasad 6 лет назад

    Outstanding !!! Your tutorial is really easy to understand, digest and develop a carrer.. Thank you

  • @TamilDove
    @TamilDove 3 года назад +1

    God bless you venkat bro.

  • @aksingh175
    @aksingh175 3 года назад +1

    sir ur best teacher at this earth,salute u

  • @panagiotisgoulas8539
    @panagiotisgoulas8539 4 года назад +5

    Yep that was hardest video up to now. Took me long to understand literally

  • @ricardoflores6827
    @ricardoflores6827 3 года назад

    much love from Florida thank you for your wonderful videos!

  • @MeMe-qx1pv
    @MeMe-qx1pv 6 лет назад

    Hi venkat.
    You are great teacher.
    I learnt from your tutorials alot.
    I have your all tutorials. C sharp sql asp mvc entity framework javascript bootstrap and much more.
    I love you so much.

  • @anuragvashishtha6660
    @anuragvashishtha6660 11 лет назад

    Thank you for another very useful session on Dates and Functions. I am surely benefited a lot by this.

  • @naveenpatil1997
    @naveenpatil1997 3 года назад

    Great tutorial!! Simple and efficient 👍

  • @splashQo
    @splashQo 7 лет назад +2

    Simply exceptional...I am speechless...thank you for this "SQL version of C# coffee machine" :D

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  7 лет назад +3

      Thank you very much for taking time to give feedback. This means a lot. I am very glad you found the videos useful.
      I have organised all the Dot Net & SQL Server videos in to playlists, which could be useful to you
      ruclips.net/user/kudvenkatplaylists?view=1&sort=dd
      If you need DVDs or to download all the videos for offline viewing please visit
      www.pragimtech.com/kudvenkat_dvd.aspx
      Slides and Text Version of the videos can be found on my blog
      csharp-video-tutorials.blogspot.com
      Tips to effectively use my youtube channel.
      ruclips.net/video/y780MwhY70s/видео.html
      If you want to receive email alerts, when new videos are uploaded, please subscribe to my youtube channel.
      ruclips.net/user/kudvenkat
      If you like these videos, please click on the THUMBS UP button below the video.
      May I ask you for a favor. I want these tutorials to be helpful for as many people as possible. Please share the link with your friends and family who you think would also benefit from them.
      Good Luck
      Venkat

  • @krishnaakella1984
    @krishnaakella1984 11 лет назад

    Thank u very very much Venkat. Its a really great work u have done!

  • @zenbabasha
    @zenbabasha 7 лет назад

    Brief and outstanding THANK YOU

  • @immanigr8
    @immanigr8 11 лет назад

    Thank you so much, giving us your valuable time.excellent GURU.

  • @raphaelbernard7954
    @raphaelbernard7954 6 лет назад

    Vankat thank you despite the fact you wrote this 5 years ago and I'm a bit late you deserve congratulations and thanks for this lesson and this series, Raph.

  • @YuusufAkorede
    @YuusufAkorede 7 лет назад

    EVEN THOUGH YOUR TUTORIALS ARE STILL VERY MUCH THE BEST

  • @johnsongeorge5906
    @johnsongeorge5906 7 лет назад

    Outstanding Sir GOD BLESS YOU

  • @maryam4071
    @maryam4071 4 года назад

    I am following all your video and I love them, thank you

  • @tareqbk1870
    @tareqbk1870 3 года назад

    what a lecture! i learned so many things from this one

  • @swatimishra7168
    @swatimishra7168 9 лет назад +2

    wonderful....its beyond perfect
    it would be great if the codes could be copied for future reference for brginners
    as I understood well but couldnt try immediately for practise

  • @mrafaqatpk
    @mrafaqatpk 2 года назад

    you are beauty, sir. Your way of teaching is amazing and very easy to understand.

  • @KedWebDev2011
    @KedWebDev2011 4 года назад

    Bravo!! Well Done!

  • @loading.m4469
    @loading.m4469 7 лет назад

    Wonderful job!

  • @garethsolomon4164
    @garethsolomon4164 2 года назад

    Awesome lesson as always.

  • @madhugarg7499
    @madhugarg7499 5 лет назад

    Simply superb tutorial

  • @kgmax4554
    @kgmax4554 8 лет назад +1

    You are awesome, Venkat. Extremely helpful. Thanks so much!

  • @krismaly
    @krismaly 9 лет назад

    Rewatched.
    Thanks

  • @tusharghude15
    @tusharghude15 6 лет назад

    Nice tutorial
    I was playing around with this example. obviously need to do all this calculation is bcoz datediff calculates when u cross the boundary of specified parameter. Now if you directly subtract 2 date times what u get is an Interval (calculated as year-month / day-time) I.e the time lapse betn 2 dates represented in one of the above format
    Let's say there is difference of 36 years 11 months 29 days 21 hrs and 46 mins betn datetime1 and datetime2 if u subtract 2 datetimes what u will get is 1936-11-29 21:46:00:000 (note: year 1900 is default value) so to get time lapse betn any 2 dates what u can do is
    SELECT ID, Name, DateOfBirth, (GETDATE()-DateOfBirth) as Interval,
    CONVERT(varchar(10), DATEDIFF(YEAR, 0,GETDATE()-DateOfBirth))+ ' Years '
    +CONVERT(varchar(10),DATEPART(MONTH,GETDATE()-DateOfBirth))+ ' Months '
    +CONVERT(varchar(10),DATEPART(DAY, GETDATE()-DateOfBirth))+ ' Days' as Age
    FROM tblEmployeeDOB
    Hope this helps

  • @pushkartripathi8860
    @pushkartripathi8860 Год назад

    Best CONTENT on SQL

  • @LurchMichal
    @LurchMichal 11 лет назад

    Really gr8 video session

  • @kaushikbhadani
    @kaushikbhadani 10 лет назад

    very very nice...

  • @krismaly6300
    @krismaly6300 9 лет назад

    Revisiting
    Thanks

  • @Mohamed-dv9ff
    @Mohamed-dv9ff 3 года назад

    Thank you very much ❤️

  • @rathnakarvlogs
    @rathnakarvlogs Год назад

    Superb brother

  • @shijujohn9969
    @shijujohn9969 3 года назад

    Nice, thanks sir

  • @nareshkumarvanga3127
    @nareshkumarvanga3127 2 года назад

    Thanks guruji

  • @shyamvishwakarma2768
    @shyamvishwakarma2768 3 года назад

    No doubt that this is the best tutorial and well organized.
    @kudvenkat - Could you please also help me with exercise questions with answers to practice query formation? please if you have it.

  • @Superman-hr4xl
    @Superman-hr4xl 10 лет назад +4

    Can someone tell me the purpose of Venkat adding the year and month back into the @tempdate?

    • @mikhailrudoy8068
      @mikhailrudoy8068 4 года назад +1

      It is for getting a new value out of @DOB=> it has a reference to @tempdate, then @tempdate recieves the value and sets it as a new sustomized value for avoiding errors in culculating, I guess

  • @stutitehri993
    @stutitehri993 10 лет назад

    good tutorial

  • @BigyanChap
    @BigyanChap 6 лет назад

    Venkat is phenomenal. I wish I could meet you.

  • @sureshpatil7005
    @sureshpatil7005 3 года назад

    Venkat Sir, Do we not have a direct function to calculate AGE in SQL ?Example: AGE (DOB)

  • @piyushtamaskar
    @piyushtamaskar 11 лет назад

    Sir I was trying to implement the logic which you explained in this particular video, But when i am executing the the function it is giving me the following error..
    "Arithmetic overflow error converting expression to data type nvarchar."

  • @lutherjohnjomaya2744
    @lutherjohnjomaya2744 4 года назад

    Wow.. how about calculations on absent and overtime?

  • @sunilkumarreddy3495
    @sunilkumarreddy3495 5 лет назад +1

    select dateadd(yyyy,2010-1900,0) means why we use 1900 for what purpose
    pls give answer about that

  • @bextla3737
    @bextla3737 7 лет назад

    Hi. it is useful. but l encounter problem with Arithmetic overflow error converting expression to data type nvarchar.
    in this case change nvarchar to int or another when create declare and set?

  • @HarpreetKaur-qq8rx
    @HarpreetKaur-qq8rx Год назад

    Hi Venkat, To calculate the difference between two dates where even though the difference was 2 months it showed 1 year can we not use month as one of the parameters instead of using case statement. Please advise.

  • @TrungNguyen-vt1nc
    @TrungNguyen-vt1nc 5 лет назад

    I really like this channel. BTW The example in this video is quite complicate, can anyone explain me that in the real world application, should we move this complicate process from SQL to C#?

  • @jawedghafory
    @jawedghafory 11 месяцев назад

    Hello Vincat brother, I really learn alot , I am searching Azure learning if available kindly let me know.

  • @sketchesbyMg
    @sketchesbyMg 3 месяца назад

    In one of the video u said that return always return int value so how is it that in function it can return nvarchar?

  • @homayounin1
    @homayounin1 8 лет назад

    hi Sir .i am trying to find 'working with variables' video but i couldnt.can you tell me which video i have to watch? thank you

  • @rutvikdeshpande3603
    @rutvikdeshpande3603 Год назад

    So lets suppose i got date diff on days and then divide it with 356 or 12 to get month and year wont that be easier?

  • @manishchoudhari1405
    @manishchoudhari1405 6 лет назад

    Hello Venkat,
    I have database ,Date_Time , ProcessVal1, ProcessVal2, ProcessVal3 . I want to filter out data by minute and Hourly .
    For report generation function. Can you please let me know . How i can do this.

  • @kunaltode2691
    @kunaltode2691 8 лет назад

    When I am executing function dbo.fnComputeAge in the end it is showing an error as 'Arithmetic overflow error converting expression to data type nvarchar.'
    Please tell me whats wrong with this

  • @blackwhite2361
    @blackwhite2361 3 года назад

    Pls tell me one thing. How did you get so good at sql?

  • @amanarora9868
    @amanarora9868 3 года назад

    Same question was asked to my friend in interview

  • @MrOwaisk
    @MrOwaisk 8 лет назад +1

    Great tutorial Venkat. Could you please provide the URL of you blog. I can't find it.
    Thanks in Advance.

  • @RakshandaMeshram
    @RakshandaMeshram 5 лет назад

    What is the datatype of column DateOfBirth in that table.tblEmployees, is it date time?

  • @thakgalomaphutha8412
    @thakgalomaphutha8412 3 года назад

    How do you deal with an interval of Half-yearly?

  • @rukminipanditi1880
    @rukminipanditi1880 2 года назад

    if we pass future date to tempvar but return age is not in currect format in above query .

  • @prosunchakraborty92
    @prosunchakraborty92 8 лет назад

    what is the purpose of @tempdate here?

  • @user-pd8wp6cv4w
    @user-pd8wp6cv4w 2 года назад

    how to find the difference between two dates in a same column using sql

  • @MDARIF-yo3tq
    @MDARIF-yo3tq 6 лет назад

    logic is quite complicated :D

  • @sudeepjanawade440
    @sudeepjanawade440 6 лет назад

    When do you use SET and SELECT statements?

    • @nys8260
      @nys8260 3 года назад

      SET is the ANSI standard way of assigning values to variables, and SELECT is not. But you can use SELECT to assign values to more than one variable at a time. SET allows you to assign data to only one variable at a time. So that in performance is where SELECT will be a winner.

  • @vizureddy6706
    @vizureddy6706 4 года назад

    how to find difference between two date columns in years, moths and days?

  • @ajaycbe0210
    @ajaycbe0210 3 года назад

    Your explanation is very good. One quick doubt, I can see that you have mentioned that days will be nvarchar(2) while converting from Integer to String, but I believe it should be nvarchar(3), please correct me if I my understanding is wrong

    • @thefrugalinvestor9392
      @thefrugalinvestor9392 2 года назад

      Dates have only 2 digits at max
      For example, 31 December ,,, at max we can have only 2 digits... not more than 2

  • @shabbirali5899
    @shabbirali5899 6 лет назад

    Sir your tutorials are so good !! sir littly i am confuse in
    select @years=datediff(YEAR,@tepdate,GETDATE())-case when
    (month(@dob)>month(GETDATE()) or (MONTH(@dob)=MONTH(GETDATE()) and (DAY(@dob)>DAY(GETDATE()))))then 1 else 0 end
    this statement or (MONTH(@dob)=MONTH(GETDATE() ... Sir why you use this.

    • @amitkumarpradhan8106
      @amitkumarpradhan8106 4 года назад

      we are adding calculated years to the dob to avoid calculating months from begging..because in this logic we are calculating years,months and days separately..try to play with this query by commenting the dateadd part in this logic, for sure u will get the clarity

  • @AnkitKumar-zn5il
    @AnkitKumar-zn5il 5 месяцев назад

    maja aaya

  • @baisakhibhattacharya7067
    @baisakhibhattacharya7067 Год назад

    Hi everyone, my output shows as follows:
    40 Years 9 Months 14634 Days old
    Is there a way to filter this in this way?
    if days > 30
    then month + 1
    and if month > 12
    then year + 1
    Thanks in advance!

  • @prachitilwankar8145
    @prachitilwankar8145 3 года назад

    why we are using 2 parameter ,@tempdate and @dob,and why we use select before @tempdate

  • @naveensharma9073
    @naveensharma9073 4 года назад

    Why we need to use @tempdate here? Why it is placed after case statement ? Why didn’t you use case Statement after calculating @days?

  • @madhugarg7499
    @madhugarg7499 5 лет назад

    please do a python video

  • @sachindev5226
    @sachindev5226 3 года назад

    Hello, I was trying to do something like SELECT DATEADD(YEAR,-7, YEAR(GETDATE())). According to me, it should subtract 7 years from the current year but it's giving me result like 1898-07-15 00:00:00.000 . Can you explain this?

    • @thefrugalinvestor9392
      @thefrugalinvestor9392 2 года назад +2

      Buddy it works like this ::
      SELECT DATEADD(YEAR, -7, GETDATE())

  • @gowthams.v6004
    @gowthams.v6004 5 лет назад

    I have a doubt why are we subtracting one in case statement. For year month and days i am not able to grasp it can someone reply me .

    • @gowthams.v6004
      @gowthams.v6004 5 лет назад

      Memeorandum of Understanding thanks so much for taking your time to reply now i understood it clearly

  • @AlishSafarli
    @AlishSafarli 4 года назад

    select DATEADD(year, @years, @tempdate)
    how do we find month with this code? it gives a year-adding

    • @amitkumarpradhan8106
      @amitkumarpradhan8106 4 года назад +1

      we are adding calculated years to the dob to avoid calculating months from begging..because in this logic we are calculating years,months and days separately..try to play with this query by commenting the dateadd part in this logic, for sure u will get the clarity

  • @ginnelasaratkumar8217
    @ginnelasaratkumar8217 4 года назад

    To Understand Step by Step using print statement
    --select datediff(year,'06/06/2019','06/05/2020')
    Declare @dob datetime , @tempdate datetime,@years int,@months int,@days int
    set @dob = '12/02/1984'
    select @tempdate = @dob
    print(@tempdate)
    select @years = datediff(year,@dob,GETDATE())-
    case
    when (month(@dob) > month(getdate())) OR
    (month(@dob) = month(getdate()) AND day(@dob) > day(getdate()))
    Then 1 else 0
    END
    print(@years) -- 35 years
    print(@tempdate)
    select @tempdate = dateadd(year,@years,@tempdate)
    print(@tempdate) --35 years added to 1984 means 1984+35=2019 i.e Dec 2 2019
    select @months = Datediff(month,@tempdate,getdate())-
    case
    when day(@dob) > day(getdate())
    then 1 else 0
    end
    print(@months) --6 months difference b/c my getdate() is 06/05/2020
    select @tempdate = dateadd(MONTH,@months,@tempdate)
    print(@tempdate) -- 6 months added to Dec 2 2019
    select @days = DATEDIFF(DAY,@tempdate,GETDATE())
    print(@days) --3 days
    select @years as year, @months as month,@days as days

  • @ohmpathi
    @ohmpathi 6 лет назад +2

    How about this query?
    declare @d1 date = '2010-12-31', @d2 date = '2011-01-01';
    declare @ds int = datediff(day, @d1, @d2);
    select @ds/365 as years, (@ds%365)/12 as months, @ds%12 days;

    • @sanjanags8465
      @sanjanags8465 4 года назад

      would you please tell me why @ds%12 is used as days

  • @YuusufAkorede
    @YuusufAkorede 7 лет назад +5

    I REALLY DON'T UNDERSTAND WHY AND HOW YOU WOULD SAY THIS IS SIMPLE.

    • @artokilponen6989
      @artokilponen6989 7 лет назад +2

      Remember that you shouldn't try to swallow the entire code at once (especially when you are studying these things) but instead make sure that you understand each and every statement that what they do. Encapsulating the code properly helps a lot with this and an example of that is the function created at the end.

  • @anilsasulkar9031
    @anilsasulkar9031 Год назад +1

    27th part video is very confusing example . I watch many times but I am not able to understand.. still trying to understand

  • @speakuparena1192
    @speakuparena1192 Год назад

    Video was blurred.

  • @subhashadhikari1433
    @subhashadhikari1433 6 лет назад

    videos with every small explanation, but i am getting python udemy ads, model looks like crow

  • @enola.94
    @enola.94 9 лет назад +1

    Hi, I am using netbeans and wampserver, it says function DATEADD does not exist whenever I run it, why is that?
    Thanks in advance!

  • @farjanamammi851
    @farjanamammi851 3 года назад

    Mat Allah reward you sir 😭😭😭❣️❣️❣️❣️

  • @_catulus
    @_catulus 3 года назад

    I was rejected by my crush on September 19th, 2012. 😹😹

  • @ramarockykrishna1598
    @ramarockykrishna1598 4 года назад

    In the last 10 mins is boring and confusing it seems un necessary topic in last 10 minutes.

  • @mylittleherodev247
    @mylittleherodev247 28 дней назад

    -select datediff(YEAR,'2020-09-02',GETDATE())---can anyone explain please these case statement
    SELECT @years = DATEDIFF(YEAR, @tempdate, GETDATE()) -CASE WHEN (MONTH(@DOB) > MONTH(GETDATE()))
    OR (MONTH(@DOB) = MONTH(GETDATE()) AND
    DAY(@DOB) > DAY(GETDATE())) THEN 1 ELSE 0 END