Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Date comparison in sql server

Posted on 2014-02-19
7
Medium Priority
?
384 Views
Last Modified: 2014-02-21
Hello,

There are two date columns in the table.

I want to compare just the Month and Year portion between those two dates with also taking the Year into consideration.

Case 1:
For Eg
DateColumn1 = '01-OCT-2013'
DateColumn2 = '01-JAN-2014'

Open in new window

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'

Open in new window

Here it should tell me that the difference is 0 as the month and the year match in this case.


Case 3:
DateColumn1 = '01-Aug-2014'
DateColumn2 = '02-JAN-2014'

Open in new window

Here it should tell me that the DateColumn2 is lesser than DateColumn1.


How to accomplish this?

Thanks!
0
Comment
Question by:sath350163
7 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 39872564
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
 

Author Comment

by:sath350163
ID: 39872571
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.

Thanks!
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 500 total points
ID: 39872592
DateDiff(month,[date_1],[date_2])

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).

also see:
http://technet.microsoft.com/en-us/library/ms189794.aspx
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39872597
Yes , Datediff function takes yesr in consideration . try out your inputs and check the results
as per yiur requirement
0
 
LVL 27

Assisted Solution

by:Zberteoc
Zberteoc earned 500 total points
ID: 39873719
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 variables
declare
	@DateColumn1 datetime,
	@DateColumn2 datetime

--case 3 returns 3 months difference, which means date2 > date1
select 
	@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&month
select 
	@DateColumn1 ='01-JAN-2014',
	@DateColumn2 ='02-JAN-2014'

select DATEDIFF(mm, @DateColumn1,@DateColumn2) as diff

--case 3 returns -7 months difference, which means date1 > date2
select 
	@DateColumn1 ='01-Aug-2014',
	@DateColumn2 ='02-JAN-2014'

select DATEDIFF(mm, @DateColumn1,@DateColumn2) as diff


-- to use with columns

select DATEDIFF(mm, DateColumn1,DateColumn2) as diff from yourtable

Open in new window

0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 500 total points
ID: 39878421
Hi,

I suggest that if you are only interested in the year/month, look at this:

select dateadd( month, datediff( month, 0, getdate()), 0 )

This will return (at present) 2014-02-01 00:00:00.0000

This is very similar code to that which removes the time from a datetime.

So, this works for years, quarters, months, days, hours, minutes - needs some adjusting for weeks, and for seconds.

HTH
  David
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question