Click here to Skip to main content
15,914,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using sql query to fetch records between range like

SQL
select * from tblLogInformation where date >='20130201'  and date <= '20130231'

so it is throwing error like

Conversion failed when converting date and/or time from character string.
because i am using 31day for every month for feb too
if i am using query like

select * from tblLogInformation where date >='20130201' and date <= '20130228'
its working fine
can some one please help me how to display records for month-wise
Posted

If you use 31 days each month, then you need stored procedure to avoid errors.
SQL
CREATE PROCEDURE GetDataFromCurrentMonth()
            @dFrom DATETIME 
--@dFrom = first day of month
AS
BEGIN
    -- last day of month
    DECLARE @dTo DATETIME

    SET @dTo = DATEADD(d,-1,DATEADD(m, 1, @dFrom))

    SELECT *
    FROM tblLogInformation 
    WHERE [date] BETWEEN @dFrom  AND @dTo
END
 
Share this answer
 
this is solution
if you want data for whole 2nd month of this year then...
SQL
select * from tbLogInformation where month(date) =2  and year(date) = 2013

Happy Coding!
:)
 
Share this answer
 
Comments
Miss Maheshwari 14-Feb-13 6:41am    
thank you very much
Aarti Meswania 14-Feb-13 6:41am    
welcome! :)
Glad to help you!
:)

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