日期范围的SQL查询引发错误 [英] sql query for date range throws error

查看:91
本文介绍了日期范围的SQL查询引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sql查询来获取范围之间的记录,例如



i am using sql query to fetch records between range like

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



因此它会抛出错误,例如



从字符串转换日期和/或时间时转换失败。 />
因为我每个月都使用31天来支付费用

如果我使用的查询类似



select * from tblLogInformation其中日期> ='''20130201''和日期< =''20130228''

它的工作正常

可以请一些人帮我看看如何显示记录月份


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!
:)


如果你每个月使用31天,那么你需要存储过程来避免错误。

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
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


这篇关于日期范围的SQL查询引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆