calculate leave year from anniversary start date - sql

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

Related

Derive 445 Quarter for any given date

I need assistance writing a query that determines the quarter of a given date based on 445 FY calendar. The FY ends on first Friday of February every year.
For example in attached image the table has the order id and order created date. Based on the order created date I want to determine which 445 FY quarter it falls in.
Use a case statement with a between condition to divide up your custom quarters. You will need to enter the start and end dates of each quarter:
Select
Case
when date between 'startdateQ1' and 'enddateQ1' then 'Quarter1'
when date between 'startdateQ2' and 'enddateQ2' then 'Quarter2'
when date between 'startdateQ3' and 'enddateQ3' then 'Quarter3'
when date between 'startdateQ4' and 'enddateQ4' then 'Quarter4'
when date is null then 'Null'
else 'Unknown'
end
As Current_Quarter
From DB

Combining year column and month column to date

In my database, I have a report_year column (char(4)) and a report_month column (varchar(2)). I am making an ssrs report that would use a stored procedure and would pull data from this table and my parameters are the date and year. I am succesful at doing this by casting both of the columns and concatenating them, also adding a "/" in between. So in SSRS report, the parameter that users need to put is the month and date (ex. 09/2016).
Users want a drop down to get the dates. Since my parameter is a varchar, it would ask literally for the month and the year formatted above. Is there anyway to cast this to date without the day itself, only just the month and the year? I tried datediff and dateadd functions but I am not having any luck.
Select BegMonth = cast(Replace('09/2016','/','/01/') as date)
,EndMonth = EOMonth(cast(Replace('09/2016','/','/01/') as date))
Returns
BegMonth EndMonth
2016-09-01 2016-09-30

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

Compare date and month in sql server for alert system

I have an a table with two columns birthday and anniversary. I want to get alerts about birthdays and anniversaries between a 7 day period of time but, that should not include year (obviously if I include year, it would always be less than the current date). I want to get the alerts 7 days in advance.
That is, the query should compare the birthday and anniversary with the current date and return a list if their birthday or anniversary falls between 7 days of the same month so that it alerts me in advance about the upcoming birthdays and anniversaries.
Subtract the year difference from now to the requested date and then use datediff to calculate the date difference of the result with the requested date.
SELECT *
FROM Table
WHERE DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Birthday,GETDATE()),GETDATE()),Birthday) BETWEEN 0 AND 7
OR DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Anniversary,GETDATE()),GETDATE()),Anniversary) BETWEEN 0 AND 7
Try This
SELECT Name,max(Table .birthdate)
FROM Table group by Table .Name having (datediff(day,max(birthdate),getutcdate())>7 and datediff(day,max(birthdate),getutcdate())<8)

How to check Day & month from date which is in Database table

I have problem here...
I want to check Day & month of date which is in database.
There is date field is in database which is mm/dd/yyyy format If I use "select" query then whole Date is access but I want only these date's which having today's day & month
Example:
if today's date is 02/02/2010 then select only those date whose day is 02 & month is February.(Dont consider Year)
Please be free to ask if you have any problem for understand.
SELECT *
FROM {tablename}
WHERE DAY({datecolumn})=DAY(getdate())
AND MONTH({datecolumn})=MONTH(getdate())
I would use DATEPART to pull out the values you're looking for.