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

I have a query to fetch out some records based on the year,In the where clause when
i am fetching on the basis of Date range i.e,1st Jan'2011 and Today's Date like
SQL
CONVERT(VARCHAR,REQDATE,101) BETWEEN CONVERT(VARCHAR,'01/01/2011',101) AND CONVERT(VARCHAR,'11/12/2011',101)

Its giving exact amount of rows but when i'm using year function like Year(a.REQDATE )= 2011
Its not giving the exact count...what could be the scenario???

Can any one help on this Please???

Thanks,
Prasant
Posted
Updated 13-Nov-11 17:31pm
v2
Comments
Prerak Patel 13-Nov-11 23:33pm    
Provide full SQL query.
User CP 13-Nov-11 23:58pm    
SELECT
DateName(month,a.REQDATE) As hp
,Month(a.REQDATE) months, count(*) as counts

FROM TASKS a INNER JOIN WorkOrderStatus b
ON a.WorkOrderStatusId = b.WorkOrderStatusId
WHERE
--Year(a.REQDATE )= 2011
--and
a.REQUEST = 'HPAD'
and
CONVERT(VARCHAR,a.REQDATE,101) BETWEEN CONVERT(VARCHAR,'01/01/2011',101) AND CONVERT(VARCHAR,'11/14/2011',101)
GROUP BY
DateName(month,a.REQDATE)
,Month(a.REQDATE)
ORDER BY
Month(a.REQDATE) ASC
Mehdi Gholam 13-Nov-11 23:51pm    
What is your reqdate data type?
User CP 13-Nov-11 23:57pm    
Its a datetime field...

Just remove the unnecessary conversion if your column is a datetime :
SQL
SELECT DateName(month,a.REQDATE) As hp ,Month(a.REQDATE) months, count(*) as counts 
FROM TASKS a INNER JOIN WorkOrderStatus b ON a.WorkOrderStatusId = b.WorkOrderStatusId WHERE 
a.REQDATE between '01-01-2011' and '11-14-2011'
GROUP BY DateName(month,a.REQDATE) ,Month(a.REQDATE) ORDER BY Month(a.REQDATE) ASC
 
Share this answer
 
Comments
Amir Mahfoozi 14-Nov-11 4:48am    
+5 I agree with you
Mehdi Gholam 14-Nov-11 5:06am    
Thanks
HI,
this is simple overview of Year function in ms-sql.
http://msdn.microsoft.com/en-us/library/ms186313.aspx[^]

as i check you sql query and i think that there is one issue with conversion of datetime field to varchar.
because it is depends upon the datetime format of sql server's system sometime it when we convert datetime field to varchar it will convert month to day and day to month so might be something like issue will be happen with your sql query and db structure.
 
Share this answer
 
And here is another approach using DATEPART :

SQL
SELECT DateName(month,a.REQDATE) As hp ,Month(a.REQDATE) months, count(*) as counts FROM TASKS a INNER JOIN WorkOrderStatus b ON a.WorkOrderStatusId = b.WorkOrderStatusId WHERE  a.REQUEST = 'HPAD' and DATEPART(yy ,a.REQDATE)=2011 GROUP BY DateName(month,a.REQDATE) ,Month(a.REQDATE) ORDER BY Month(a.REQDATE) ASC
 
Share this answer
 
Comments
Mehdi Gholam 14-Nov-11 5:07am    
5'ed
Amir Mahfoozi 14-Nov-11 6:54am    
Thank you
Use DateDiff function for this


Select ..... from <yourtablename> where Datediff(yy,<startdate>,<enddate>)


For more information go through this link

http://www.sqlteam.com/article/datediff-function-demystified[^]

http://blog.sqlauthority.com/2011/10/21/sql-server-datediff-accuracy-of-various-dateparts/[^]
 
Share this answer
 
See this sql between

see below text in this link
<br />
Note: The BETWEEN operator is treated differently in different databases!<br />
<br />
In some databases, persons with the LastName of "Hansen" or "Pettersen" will not be listed, <br />
<br />
because the BETWEEN operator only selects fields that are between and excluding the test <br />
<br />
values.<br />
<br />
In other databases, persons with the LastName of "Hansen" or "Pettersen" will be listed, <br />
<br />
because the BETWEEN operator selects fields that are between and including the test values.<br />


So try with this solutions
SQL
1. a.REQDATE between '01-01-2011' and '11-14-2011'
2. a.REQDATE between '31-12-2010' and '11-15-2011' 
 
Share this answer
 

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