sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Date and Time popular calculations

Antonios Chatzipavlis
Tuesday 26 September 2017

Δεν είναι λίγες οι φορές που στα σεμινάρια που κάνω ειδικά στην T-SQL οι μαθητές μου με ρωτάνε για το πως να υπολογίσουν διάφορα πράγματα με τις ημερομηνίες.

Επίσης βλέπω αρκετούς να βασανίζονται στα queries που γράφουν για να κάνουν τους διάφορους υπολογισμούς με τρόπους χαοτικούς και δαπανηρούς στην εκτέλεση.

Για αυτό το λόγο σε αυτό το μικρό άρθρο σας παραθέτω το πως μπορείτε να υπολογίσετε γρήγορα και με σχεδόν μηδενικό κόστος τις δημοφιλέστερες ερωτήσεις με τις ημερομηνίες


declare @givendate datetime = GETDATE();

-- all sql server versions

select    @givendate as CurrentDate
    ,    (YEAR(@givendate) * 100 ) + MONTH(@givendate)    as YYYYMM; -- return type int

-- before sql server 2012

select    @givendate as CurrentDate
    ,    DATEADD(MONTH,DATEDIFF(MONTH,0, @givendate),0) as FirstDayCurrentMonth 
    ,    DATEADD(DAY,-1, DATEADD(MONTH,DATEDIFF(MONTH,0,@givendate)+1,0)) as LastDayCurrentMonth
    ,    DATEADD(MONTH,DATEDIFF(MONTH,0,@givendate)-1,0) as FirstDayPreviousMonth 
    ,    DATEADD(DAY,-(DAY(@givendate)),@givendate) as LastDayPreviousMonth
    ,    DATEADD(MONTH,DATEDIFF(MONTH,0,@givendate)+1,0) as FirstDayNextMonth 
    ,    DATEADD(DAY,-1 , DATEADD(MONTH,DATEDIFF(MONTH,0,@givendate)+2,0)) as LastDayNextMonth;

-- sql server 2012 and above

select    @givendate as CurrentDate
    ,    DATEADD(DAY,1,EOMONTH(@givendate,-1)) as FirstDayCurrentMonth
    ,    EOMONTH(@givendate) as LastDayCurrentMonth
    ,    DATEADD(DAY,1,EOMONTH(@givendate,-2)) as FirstDayPreviousMonth
    ,    EOMONTH(@givendate,-1) as LastDayPreviousMonth
    ,    DATEADD(DAY,1,EOMONTH(@givendate)) as FirstDayNextMonth
    ,    EOMONTH(@givendate,1) as LastDayNextMonth;

-- all sql server versions

select    @givendate as CurrentDate
    ,    DATEADD(YEAR,DATEDIFF(YEAR,'19000101',@givendate),'19000101') as FirstDayCurrentYear
    ,    DATEADD(DAY,-1, DATEADD(YEAR,DATEDIFF(YEAR,'19000101',@givendate)+1,'19000101')) as LastDayCurrentYear
    ,    DATEADD(YEAR,DATEDIFF(YEAR,'19000101',@givendate)-1,'19000101') as FirstDayPreviousYear
    ,    DATEADD(DAY,-1, DATEADD(YEAR,DATEDIFF(YEAR,'19000101',@givendate),'19000101')) as LastDayPreviousYear
    ,    DATEADD(YEAR,DATEDIFF(YEAR,'19000101',@givendate)+1,'19000101') as FirstDayNextYear
    ,    DATEADD(DAY,-1, DATEADD(YEAR,DATEDIFF(YEAR,'19000101',@givendate)+2,'19000101')) as LastDayNextYear;

-- all sql server versions

select    @givendate as CurrentDate
    ,    DATEADD(SECOND,(60*60*24)-1,DATEADD(DAY,0,DATEDIFF(DAY,0,@givendate))) as MidnightToday
    ,    DATEADD(SECOND,(60*60*24)-1,DATEADD(DAY,-1,DATEDIFF(DAY,0,@givendate))) as MidnightYesterday
    ,    DATEADD(SECOND,(60*60*24)-1,DATEADD(DAY,1,DATEDIFF(DAY,0,@givendate))) as MidnightTommorow
    ,    DATEADD(HOUR,12,DATEADD(DAY,0,DATEDIFF(DAY,0,@givendate))) as NoonToday
    ,    DATEADD(HOUR,12,DATEADD(DAY,-1,DATEDIFF(DAY,0,@givendate))) as NoonYesterday
    ,    DATEADD(HOUR,12,DATEADD(DAY,1,DATEDIFF(DAY,0,@givendate))) as NoonTommorow


//antonch


Leave your comment

 


 


   




   
captcha

 

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.

Episode

Row-level Security in SQL Server

More Episodes...

Tip

Refresh Intellisence in SSMS

Για να κάνουμε refresh το intellisence μέσα στο SSMS αρκεί να πατήσουμε Ctrl+Shift+R

More Tips...

Info graph

a day in data

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2023 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.