Click here to Skip to main content
15,884,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using the date below;

SN NAME STARTDATE ENDDATE

1. BABS 01/04/2011 03/06/2012
2. JKK 10/10/2011 23/12/2012
3. HAN 04/04/2011 06/02/2012
4. LOL 16/05/2011 05/05/2012

Assuming am search for record that has month and year of 05/2011

With the following code;

SELECT * FROM mytable WHERE 05 BETWEEN MONTH(StartDate) AND MONTH(EndDate) AND
(YEAR(StartDate) = 2011 OR YEAR(EndDate) = 2011)

Executing the statement above the record shown is

SN NAME STARTDATE ENDDATE

1. BABS 01/04/2011 03/06/2012
2. JKK 10/10/2011 23/12/2012
4. LOL 16/05/2011 05/05/2012


Record 3 is not showing, which suppose to be included. Looking at Record 3. the month(startdate) is less then the month(enddate) (04<02) How can I handle a record where the starting month is less than the ending month?
Posted

http://www.w3schools.com/sql/sql_between.asp[^] In other words, you can say you want the date between two dates explicitly.
 
Share this answer
 
Comments
harish85 9-Jul-11 2:49am    
Nice link, I never noticed w3schools had links apart from html or html related technologies.
Thanks. <5.
This is just alternate "idea" that you could use..

select * from mytable where (DATEDIFF(yy,StartDate,EndDate)*1000 + DATEDIFF(mm,StartDate,EndDate)) > 2004

(Date difference greater than 2 yr and 4 months)

or

get "YYYYMM" together to compare

Disclaimer - these are only general ideas- non technology specific.
 
Share this answer
 
Comments
Christian Graus 9-Jul-11 3:27am    
But she doesn't want to check the size of the date difference, she wants the value to be inside a range.
harish85 9-Jul-11 3:36am    
nevermind.still didnt get OP's problem, either I am too tired, or I am not understanding it. Sorry about that post.
btw how did you make out OP as "She" and "He" ?
Christian Graus 9-Jul-11 3:46am    
I assumed Kay is a girls name, could be wrong.
You can also uses tool to builds you SQL request like those in Visual Studio or LINQPad and then look at the generated SQL.
 
Share this answer
 
Christain Tks, but that is not applicable to date. In a situation the searching month falls between two dates of the same year the sql query work, but when with diffrent years and also if the month of starting date is less than month of end date it does not work
 
Share this answer
 
Comments
Christian Graus 9-Jul-11 3:18am    
Please don't push 'answer' to ask questions. You are wrong. The between keyword works with dates. Rereading your code, your issue is that you're using the between keyword only on the month, and adding a check for 2011, which is obviously not future proof. Use the Between keyword on the entire dates, against the full date object you want to compare, and it will work. If you ask for advice, do what people advise you before telling them that they, and all the online documentation, are wrong.
Philippe Mori 9-Jul-11 13:45pm    
As mentionned by Christian, you must compare the whole date at once.
Narish85 Thanks, This is not working at all
 
Share this answer
 
Comments
Christian Graus 9-Jul-11 3:44am    
Because it does not solve your problem. My answer does. Again, DON'T PUSH ANSWER TO ADD COMMENTS !!! If you can't follow basic rules of etiquette, or follow basic instructions, then what is the point in us trying to help you ? See how harish85 and I both use the 'comment' button to add comments ? you can do it. Learn something !! Try to behave acceptably and follow the rules of the site !!!
Christian Graus 9-Jul-11 4:27am    
I can see now you've asked this twice, and despite being told exactly what to do, you insist on doing it wrong, then asking us to fix it. Is this some sort of practical joke ? Why can't you just do what you've been told ?

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