Hi guys i have a table expenses with multiple records.
expenseID ratename description workcompleted
51699 Base Rate SNL Financial inquiry 08/09/2011
51699 Base Rate SNL Financial inquiry 08/19/2011
51699 Base Rate SNL Financial inquiry 08/09/2012
51699 Base Rate SNL Financial inquiry 08/19/2012
But when i search record only for year 2012 but it return record for both the year(2011, 2012). But I need my record comes by year only i.e.
expected results:
51699 Base Rate SNL Financial inquiry 08/09/2012
51699 Base Rate SNL Financial inquiry 08/19/2012
Please see my query :
select expenseid,ratename,description,workcompleted from EXPENSES
where workcompleted >= '08/08/2012' and workcompleted <= '08/19/2012'
I also try this by using between query but it doesn’t work. If any body have an idea kindly share to me.
You’re almost certainly using the wrong data type. Your column “work_completed” should be of type date. It’s almost certainly char or varchar. Your best bet is to change the data type.
If you can’t do that, you can create a view that mirrors the table, but casts the “work_completed” column to a date type. Querying a date range against such a view should work correctly.
If you can’t create views, you can change your WHERE clause to do the cast. In standard SQL, it would look like
I know SQL Server supports CAST(), but you’ll probably have to go with your original format for the dates (like ’08/08/2012′).
Expect a performance hit if you do that. SQL Server will have to do two type casts on every row.