SQL: Creating Dynamic Fiscal Quarters - sql

I have a set of data which includes an account #, and a fiscal end date. The fiscal end date does not always match the calendar year. I need to take the given fiscal end date and generate the quarters based on it. For example, if fiscal end date equals 6/31 then the first quarter would be 7/01 thru 9/30, second would be 10/1 thru 12/31, etc.
The only way I can currently think to do this would be a very long and complicated if/then structure. Any help making something more efficient would be greatly appreciated.

You can use DATEADD() function
select convert(varchar(10),dateadd(day,1,'6/25/2016'),101) as startdate,
convert(varchar(10),dateadd(month,3,dateadd(day,1,'6/25/2016')),101) as enddate
Result will be like:

Related

SQL if last day of pervious month has value change the beginning date of next month to start from that day

Need help with this sql code, I would like to roll back start day of new month if last day of pervious month has values.
Thanks
I would suggest nesting EOMONTH() in an IF statement.
Not sure what table(s) you're working with, but general layout would be:
IF(MAX(date_column) = EOMONTH(MAX(date_column), MAX(date_column), new_date)
Reference for EOMONTH():
https://www.sqlservertutorial.net/sql-server-date-functions/sql-server-eomonth-function/

Using MDX in SSAS tabular get first 10 days of each month

I am trying to dynamically get the dates, starting from begging of month to current date using SSAS Tabular - DAX, I have tried many date functions but I couldn't end up with solution, so is there any idea to share pls ?
The following DAX function will return a table of DATES for each day from the start of the current month through today:
DatesMTD = CALENDAR(DATE(YEAR(TODAY()), MONTH(TODAY()), 1), TODAY())
To be able to filter the dates for the first 10 days, create a calculated column that identifies as a date in this range
1st to 10th Date = If(Day([Date]) <11,1,0)
This can then be used to either filter out the dates past the 10th of each month

How to extract month, year and date from Date level in MDX

It is my first time working with MDX in JPivot, I have a Date level (Format: YYYY-MM-dd) and I have to do some queries based on the year or month of this date. I did not find a way to extract this separately.
This is my query :
select
[Person].[job].Members ON COLUMNS,
[WorkDate].[Date].Members ON ROWS
from [Work];
Is there a way to do it?
Thank you in advance!
If the [Workdate] hierarchy has Year and Month levels, you can access those by using
[Workdate].CurrentMember.Parent.Parent.Name
(assuming Year is the grand-parent level of Date)
or
Ancestor( [Workdate].CurrentMember, [Workdate].[Year]).Name
If you don't have a Year/Month/Date hierarchy then you'll need to break the date string into bits using [Workdate].CurrentMember.Name and string functions.

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

calculate leave year from anniversary start date

I am trying to calculate someone holiday year start and end dates based on the anniversary of their joining date.
For example if someones joining date is 23/10/09 i need two fields calculated from this date which would be 23/10/13 for the start of their holiday year and the end of their leave year would be 22/10/14.
Basically i need a query that looks at the dd/mm of the joining date and the current date and calculates the two dates i require.
Another example would be joining date 01/02/10 and the start date of the leave year would be 01/02/14 and the end date date would be 31/01/15. The field name in my SQL database is
[emp_join_date]
Any ideas ?
DATEADD() would be the function you need I believe:
declare #emp_join_date datetime = '2009-10-23'
select #emp_join_date as emp_join_date,
DATEADD(YEAR,4,#emp_join_date) as holiday_start_date,
DATEADD(YEAR,5,#emp_join_date)-1 as emp_end_date