Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to get the Year Period as per the currentDate(GetDate()) depend upon Joining Date.

For Example.

Emp_No     Joining Date        As per todaycurrentDate(His One year period End)
 
1                14-Jul-2013                14-Jul-2017
2                14-Jul-2016               14-Jul-2017 
3                01-Feb-2015               01-Feb-2017


How to do the query?

What I have tried:

I use the case but this one will not suite.

CASE WHEN JoinDate < GetDate() THEN dateadd(DAY , 365 , JoinDate) ELSE JoinDate END

Thanks
Basit.
Posted
Updated 18-Feb-17 23:26pm
v2

Try:
SQL
CASE WHEN JoinDate < GetDate() THEN DATEFROMPARTS(DATEPART(yy, GETDATE()), DATEPART(mm, JoinDate), DATEPART(dd, JoinDate)) ELSE JoinDate END
 
Share this answer
 
Comments
basitsar 19-Feb-17 9:00am    
Many Thanks
OriginalGriff 19-Feb-17 9:02am    
You're welcome!
Don't know, what's wrong with your solution except that leap years does'nt have 365 days. If I would write that query it would look like this

SQL
SELECT [Emp_No]
     , [JoinDate]
     , CASE
         WHEN ([JoinDate] < getdate()) THEN DATEADD(YEAR, 1, [JoinDate])
         ELSE [JoinDate]
       END AS [PeriodEnd]
FROM [dbo].[<Tablename>]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900