Solved

SQL Date Range Syntax Assistance

Posted on 2016-08-19
3
44 Views
Last Modified: 2016-08-22
I have the need to calculate a 'season to date' date range and add it to my query below. Our season runs as follows January thru Jun and July thru December so if the current month is August my output should be 07/01/2016 AS STDStartDate and 12/31/2016 AS STDEndDate

SELECT  DATEADD(wk, 0,
                DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()),
                        DATEDIFF(dd, 0, GETDATE()))) AS WkStartDate ,
        DATEADD(wk, 1,
                DATEADD(DAY, 0 - DATEPART(WEEKDAY, GETDATE()),
                        DATEDIFF(dd, 0, GETDATE()))) AS WkEndDate ,
        DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AS MthStartDate ,
        DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0)) AS MthEndDate ,
        DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS YearStartDate ,
        DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS YearEndDate;

Open in new window

0
Comment
Question by:mburk1968
3 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41762632
Sounds like a candidate for building your own SQL Server Calendar Table, where you can add logic for custom seasons like Fiscal Years here.  

That way instead of having to code a big honkin' expression every time you want to query season-to-date, just code it once when you build the table in order to populate season-specific columns.  Then you can do a simple JOIN in query expressions.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41763863
May be something like this. try this.
SELECT  DATEADD(wk, 0,
                DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()),
                        DATEDIFF(dd, 0, GETDATE()))) AS WkStartDate ,
        DATEADD(wk, 1,
                DATEADD(DAY, 0 - DATEPART(WEEKDAY, GETDATE()),
                        DATEDIFF(dd, 0, GETDATE()))) AS WkEndDate ,
        DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AS MthStartDate ,
        DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0)) AS MthEndDate ,
        DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS YearStartDate ,
        DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS YearEndDate,
        CASE WHEN REPLACE(CONVERT(VARCHAR(5),GETDATE(),1),'/','') <= '0630' THEN '0101' ELSE '0701' END AS STDStartDate,
        CASE WHEN REPLACE(CONVERT(VARCHAR(5),GETDATE(),1),'/','') <= '0630' THEN '0630' ELSE '1231' END AS STDEndDate;

Open in new window

0
 

Author Closing Comment

by:mburk1968
ID: 41765447
Thank you
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question