如何在此SQL查询中显示过去20天

时间:2014-04-01 20:11:24

标签: c# asp.net sql sql-server

我想在最后更改标准。到过去的20天。 这是 ? (DateTime.Now.Year) - 20?

  sqlT1 = "SELECT ROW_NUMBER() OVER(ORDER BY ID_KEY DESC) AS RN,* From(Select distinct f.FACILITY_NAME, ID_KEY, [BATCH] AS column1, [IMPORTDATE], [DATEBILLED], [RX], [DATEDISPENSED], [DAYSUPPLY], [PAYTYPE], [NPI], [PHYSICIAN], [COST], [QUANTITY], [MEDICATION], A.[NDC], " +
                        " case when (COST > 0 AND DAYSUPPLY > 0) then (COST / DAYSUPPLY) * 30 else 0 end [30DayCost] , [PATIENTNAME], [ROUTEOFADMIN], [INVOICECAT], [COPAY], [BRAND], [TIER], [SKILLLEVEL], [STAT] STATUS, [LASTTASKDATE],SEQNO, 'please bring to the attention of the administrator.' SUBST_INSTRUCTIONS  , f.FACILITY_ID " +
                        " FROM [LMI].[T_CHARGES] A Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI  " +
                        " Where COST > 500 " +
                        " AND [TIER] = 'T1' and month(A.DATEDISPENSED) = " + DateTime.Now.Month + " and year(A.DATEDISPENSED) = " + DateTime.Now.Year + "" +
                        sqlWhere + " AND f.FACILITY_ID IN (" + selected + ")";

2 个答案:

答案 0 :(得分:1)

我不知道如何为您更改代码,但通常在SQL中使用getdate()来获取今天的日期,并且您使用dateadd(d,-20,getdate())来获取20天前的代码使用

(T-SQL):

 testingdate between dateadd(d,-20,getdate()) and getdate()
在你的where子句中

答案 1 :(得分:0)

要获取20天前的日期,您可以使用sql函数DATEADD:

WHERE A.DATEDISPENSED >= DATEADD(dd,-20,GetDate())