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

i have two combo box where i added all the months name's first 3 letters(e.g. Jan, Feb etc.)
and another combo box which is having all the years from 2005-2012

Now i wrote the below query to find the result between 2 months of a year
but it's not giving me the output.

Please tell me where i am going wrong

SQL
select DatePart(MM,FromDate) as Month,Sum(Amount) as Amount 
from SuppReport where DatePart(MM,FromDate) between 
DATEPART(MM,CAST('Jan'+ '2005' AS datetime) and DATEPART(MM,CAST('Dec'+ '2005' AS datetime)
and CONVERT(VARCHAR(4),
DateName(YEAR,FromDate))='2005' Group By DatePart(MM,FromDate)


In this query i am trying to convert month name to month number.
but showing error

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'and'.
Posted
Updated 5-Dec-13 17:45pm
v3

1 solution

Use DATEPART instead, see here : http://www.w3schools.com/sql/func_datepart.asp[^]

and also you should use numeric months not 'Feb' and 'Apr' (you can do it with the DATEPART function).
 
Share this answer
 
Comments
[no name] 5-Dec-13 7:39am    
Thanks Mehdi. but i have to do like this only..please help me out..
Mehdi Gholam 5-Dec-13 7:44am    
Why?
[no name] 5-Dec-13 7:45am    
Now this is how my company ppl asked me to do..can't help it..They already did single month, year and date wise..i have to add from and to function
Mehdi Gholam 5-Dec-13 7:54am    
Just change the where clause to work with DatePart, it's simpler and works better.
[no name] 5-Dec-13 7:56am    
Then how to convert the month from lets say Jan to 01

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