When I compare the Month and year in DateColumn1 against DateColumn2, it should tell me that DateColumn1 is less (i.e, earlier) than DateColumn2, even though the Month October in DateColumn1 is a later month compare to Jan, theDatColumn1 is in the past year and DateColumn2 is in the current year.
Case 2:DateColumn1 = '01-JAN-2014'DateColumn2 = '02-JAN-2014'
Not sure in what format you want the answer .
As I understand you want to compare two dates and need result like which is less , or same depending on month and yesr
try this - hope it helps
Declare @date_col1 as datetime
declare @date_col2 as datetime
set @date_col1 = '01-Aug-2014'
set @date_col2 = '02-JAN-2014'
select
Case when datediff(month,@date_col1,@date_col2) < 0 then 'date col 2 is less'
when datediff(month,@date_col1,@date_col2) > 0 then 'date col 1 is less'
else ' both equal ' end
0
sath350163Author Commented:
Thanks for the response.
But I also want the year to be taken into consideration for decision on whether the date is lesser or greater.
will take the year into account, what the 'month' is doing in that function is identifying the unit of precision (month), but it is still comparing full date values (i.e. they already include the year).
I can give you an example for each of the cases using the DATEDIFF function that will return the difference in months between 2 dates.
1. If the number is positive means that the @DateColumn2 is bigger than @DateColumn1
2. If the number = 0 (0 months difference) means teh Year and Month are the same
3. If the number is negative means that @DateColumn1 is bigger than @DateColumn2
--declare variablesdeclare @DateColumn1 datetime, @DateColumn2 datetime--case 3 returns 3 months difference, which means date2 > date1select @DateColumn1 ='01-OCT-2013', @DateColumn2 ='01-JAN-2014'select DATEDIFF(mm, @DateColumn1,@DateColumn2) as diff--case 2 returns 0 months difference, which means date1 Year&month = > date2 Year&monthselect @DateColumn1 ='01-JAN-2014', @DateColumn2 ='02-JAN-2014'select DATEDIFF(mm, @DateColumn1,@DateColumn2) as diff--case 3 returns -7 months difference, which means date1 > date2select @DateColumn1 ='01-Aug-2014', @DateColumn2 ='02-JAN-2014'select DATEDIFF(mm, @DateColumn1,@DateColumn2) as diff-- to use with columnsselect DATEDIFF(mm, DateColumn1,DateColumn2) as diff from yourtable
The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
As I understand you want to compare two dates and need result like which is less , or same depending on month and yesr
try this - hope it helps
Declare @date_col1 as datetime
declare @date_col2 as datetime
set @date_col1 = '01-Aug-2014'
set @date_col2 = '02-JAN-2014'
select
Case when datediff(month,@date_col1,
when datediff(month,@date_col1,
else ' both equal ' end