我需要在 where 语句中动态设置日期,以仅从 table 中提取在县财政年度发生的结果
I need to dynamically set the date in a where statement to only pull results from a table to occur in County's fiscal year
这是我当前的代码:
SELECT nature, inci_id, calltime, agency, gp
FROM inmain AS inmain
WHERE (calltime >= CONVERT(DATETIME, '2013-07-01 00:00:00', 102))
AND (calltime <= CONVERT(DATETIME, '2014-06-30 23:59:59', 102))
ORDER BY nature, calltime
我需要它在每个财政年度结束时提取数据,而不是一直进入并更改日期,但我不知道如何让它只是改变所以它在 7/1/ 之间(上一年)至 6/30/(当年)。
我们使用自动化程序在财政年度结束时将其发送出去。这样我就不必一直手动操作了。
我想你想做的是:
SELECT nature, inci_id, calltime, agency, gp
FROM inmain AS inmain
WHERE (calltime >= CONVERT(DATETIME, CAST(YEAR(GETDATE()) - 1 AS VARCHAR(10)) + '-07-01 00:00:00', 102))
AND (calltime <= CONVERT(DATETIME, CAST(YEAR(GETDATE()) AS VARCHAR(10)) + '-06-30 23:59:59', 102))
ORDER BY nature, calltime
但您也可以使用变量来使代码更具可读性,如下所示:
DECLARE @CurrentYear INT = 2015
DECLARE @CurrentYearAsString VARCHAR(10) = CAST(@CurrentYear AS VARCHAR(10))
DECLARE @PreviousYearAsString VARCHAR(10) = CAST(@CurrentYear - 1 AS VARCHAR(10))
SELECT nature, inci_id, calltime, agency, gp
FROM inmain AS inmain
WHERE (calltime >= CONVERT(DATETIME, @PreviousYearAsString + '-07-01 00:00:00', 102))
AND (calltime <= CONVERT(DATETIME, @CurrentYearAsString + '-06-30 23:59:59', 102))
ORDER BY nature, calltime
使用 datepart year,这样你就不会对函数执行搜索我建议设置一些变量,这些变量将在主查询之前设置 运行,所以你的 sarg 是针对日期的。
declare @fiscalStart = select cast('07/01/' +
Cast((datepart(year, getdate())-1) as varchar(4)) as datetime)
declare @fiscalEnd = select dateadd(second, -1, cast('07/01/' +
Cast(datepart(year, getdate()) as varchar(4)) as datetime))
SELECT nature, inci_id, calltime, agency, gp
FROM inmain AS inmain
WHERE calltime between @fiscalStart and @fiscalEnd
ORDER BY nature, calltime
这是我当前的代码:
SELECT nature, inci_id, calltime, agency, gp
FROM inmain AS inmain
WHERE (calltime >= CONVERT(DATETIME, '2013-07-01 00:00:00', 102))
AND (calltime <= CONVERT(DATETIME, '2014-06-30 23:59:59', 102))
ORDER BY nature, calltime
我需要它在每个财政年度结束时提取数据,而不是一直进入并更改日期,但我不知道如何让它只是改变所以它在 7/1/ 之间(上一年)至 6/30/(当年)。
我们使用自动化程序在财政年度结束时将其发送出去。这样我就不必一直手动操作了。
我想你想做的是:
SELECT nature, inci_id, calltime, agency, gp
FROM inmain AS inmain
WHERE (calltime >= CONVERT(DATETIME, CAST(YEAR(GETDATE()) - 1 AS VARCHAR(10)) + '-07-01 00:00:00', 102))
AND (calltime <= CONVERT(DATETIME, CAST(YEAR(GETDATE()) AS VARCHAR(10)) + '-06-30 23:59:59', 102))
ORDER BY nature, calltime
但您也可以使用变量来使代码更具可读性,如下所示:
DECLARE @CurrentYear INT = 2015
DECLARE @CurrentYearAsString VARCHAR(10) = CAST(@CurrentYear AS VARCHAR(10))
DECLARE @PreviousYearAsString VARCHAR(10) = CAST(@CurrentYear - 1 AS VARCHAR(10))
SELECT nature, inci_id, calltime, agency, gp
FROM inmain AS inmain
WHERE (calltime >= CONVERT(DATETIME, @PreviousYearAsString + '-07-01 00:00:00', 102))
AND (calltime <= CONVERT(DATETIME, @CurrentYearAsString + '-06-30 23:59:59', 102))
ORDER BY nature, calltime
使用 datepart year,这样你就不会对函数执行搜索我建议设置一些变量,这些变量将在主查询之前设置 运行,所以你的 sarg 是针对日期的。
declare @fiscalStart = select cast('07/01/' +
Cast((datepart(year, getdate())-1) as varchar(4)) as datetime)
declare @fiscalEnd = select dateadd(second, -1, cast('07/01/' +
Cast(datepart(year, getdate()) as varchar(4)) as datetime))
SELECT nature, inci_id, calltime, agency, gp
FROM inmain AS inmain
WHERE calltime between @fiscalStart and @fiscalEnd
ORDER BY nature, calltime