Click here to Skip to main content
14,694,017 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

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

this is solution
if you want data for whole 2nd month of this year then...
select * from tbLogInformation where month(date) =2  and year(date) = 2013

Happy Coding!
Miss Maheshwari 14-Feb-13 6:41am
thank you very much
Aarti Meswania 14-Feb-13 6:41am
welcome! :)
Glad to help you!
If you use 31 days each month, then you need stored procedure to avoid errors.
CREATE PROCEDURE GetDataFromCurrentMonth()
            @dFrom DATETIME 
--@dFrom = first day of month
    -- last day of month

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

    SELECT *
    FROM tblLogInformation 
    WHERE [date] BETWEEN @dFrom  AND @dTo

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