Question

I have a query that returns a date. eg 2013-10-12.

I want to check if the date is between June 1st of the year and the last day of the year.

If it is between these dates increase the year by 1 else leave the year as it is.

So, taking the example above 2013-10-12 the result I want is 2014. If it was 2013-01-12 the result I want is 2013.

I hope this makes sense.

Was it helpful?

Solution

Declare @Yourdate datetime = '2013-10-13'

SELECT CASE WHEN 
(@Yourdate between '2013-06-01' and  DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1) ) 
THEN YEAR(@Yourdate) + 1 ELSE (YEAR(@Yourdate) ) END AS [YEAR]

OTHER TIPS

declare @arg datetime
set @arg = '01/12/2013'

select 
case when month(@arg) > 5 then year(@arg) + 1 else year(@arg) end
declare @var varchar(20) = '2013-01-12';

select case when Month(convert(datetime,@var ,120)) > 5 then Year(convert(datetime,@var ,120)) + 1 else Year(convert(datetime,@var ,120)) end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top