日期范围的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