I have a problem where i have a simple sql query as displayed below:
我有一个问题,我有一个简单的SQL查询,如下所示:
Select
Ah_editime as todaysdate,
(CONVERT(VARCHAR(25), DATEADD(dd, -(DAY(GETDATE()) - 1), GETDATE()), 103)) AS monthstartdate,
(CONVERT(VARCHAR(10), CAST(GETDATE() AS DATE), 103)) AS monthcurrentdate
from
Transaction
where
Ah_editime BETWEEN (CONVERT(VARCHAR(25), DATEADD(dd, -(DAY(GETDATE()) - 1), GETDATE()), 103))
AND (CONVERT(VARCHAR(10), CAST(GETDATE() AS DATE), 103))
I want to display result only for current month to till date. But the problem that I face is I get the values from past month as well which creates issues. I have a report that displays the values.
我想只显示当前月份的结果。但是我面临的问题是我从过去一个月获得的价值也会产生问题。我有一个显示值的报告。
Here if you see I am getting all the values but I want for the current month only.
在这里,如果你看到我得到所有的价值,但我只想在当月。
P.S : Can that be the format issue? todaysdate
that is getting displayed which is my actual value.
P.S:这可能是格式问题吗?今天更新显示的是我的实际价值。
2 个解决方案
#1
2
Date
format should be modified as below in your query. Instead of 103 use 101.
日期格式应在查询中修改如下。而不是103使用101。
BETWEEN (CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101)) AND (CONVERT(VARCHAR(10), CAST(GETDATE() AS DATE),101))
#2
0
Based on this post, what about following query :
根据这篇文章,以下查询如何:
DECLARE @monthStartDate AS DATE
DECLARE @monthCurrentDate AS DATE
SELECT @monthStartDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
SELECT @monthCurrentDate = GETDATE()
SELECT CAST(Ah_editime AS DATE) AS todaysdate ,
@monthStartDate AS MonthStartDate ,
@monthCurrentDate AS MonthCurrentDate
FROM [Transaction]
WHERE Ah_editime BETWEEN @monthStartDate AND @monthCurrentDate
#1
2
Date
format should be modified as below in your query. Instead of 103 use 101.
日期格式应在查询中修改如下。而不是103使用101。
BETWEEN (CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101)) AND (CONVERT(VARCHAR(10), CAST(GETDATE() AS DATE),101))
#2
0
Based on this post, what about following query :
根据这篇文章,以下查询如何:
DECLARE @monthStartDate AS DATE
DECLARE @monthCurrentDate AS DATE
SELECT @monthStartDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
SELECT @monthCurrentDate = GETDATE()
SELECT CAST(Ah_editime AS DATE) AS todaysdate ,
@monthStartDate AS MonthStartDate ,
@monthCurrentDate AS MonthCurrentDate
FROM [Transaction]
WHERE Ah_editime BETWEEN @monthStartDate AND @monthCurrentDate