0

我还是 SQL 的新手,所以我问的这个问题对你来说可能很容易。因此,我正在创建一份报告,该报告每周都会生成前 14 天(或 2 周)的资助合同。我知道这必须硬编码到特定的公司。该特定公司的 ID 是 '55' 那么有人可以帮我完成这个功能吗?我知道我的查询尚未完成我只是停留在如何为此输入日期函数。

Create PROC [dbo].[spAdminFundedDateee]

   Begin

        SELECT  c.program_id, d.dealer_code,b.last_name As DealerName, a.account_no, a.vin, 
                ((e.last_name)+','+(e.first_name)) As Name, a.funded_date, a.cancel_refund_date,
                  a.purchase_date,a.miles, a.duration,a.sale_price,a.number_of_payments,
                  a.sales_tax, a.downpayment

        from tDealer d 
    Join tContact b ON d.contact_id = b.contact_id 
    Join tContract a On d.dealer_id = a.dealer_id 
    Join tCompany c ON d.company_id= c.company_id
    Join tContact E On e.contact_id = a.contact_id

        Where c.program_id = 55 And a.funded_date between 

      End

    exec spAdminFundedDateee '05/1/2014','05/30/2014','55'
4

3 回答 3

0

嘿伙计们,所以我更新了我的 Where 子句,但是当我运行查询时,我得到了列标题但有一个空表。当我输入日期的硬代码时是否正确?我仍然是 Date 函数的新手,所以我知道这对你们大多数人来说一定比对我来说是常识。我想硬编码 14 天之前的日期,并假设每两周在 14 天之前的每个星期一获得资金合同。这是我的第一份报告,所以对于提出一些你们可能认为我应该知道的问题,我深表歉意。我仍在学习。

      Alter PROC [dbo].[spAdminFundedDateee]
      As
         Begin
             SELECT  c.program_id, d.dealer_code,b.last_name As DealerName, a.account_no, 
                     a.vin,  ((e.last_name)+','+(e.first_name)) As Name, 
                     a.funded_date, a.cancel_refund_date, a.purchase_date,a.miles, 
                     a.duration,a.sale_price,a.number_of_payments,  a.sales_tax, a.downpayment


      from tDealer d 
       Join tContact b ON d.contact_id = b.contact_id 
       Join tContract a On d.dealer_id = a.dealer_id 
       Join tCompany c ON d.company_id= c.company_id
       Join tContact E On e.contact_id = a.contact_id


     Where c.program_id = 55 And  a.funded_date between '05/19/2014' and '06/02/2014'And 
           a.funded_date between  dateadd(dd, -14, cast(getDate() as date)) and cast(getDate()
           as date)


     END
     GO

    EXEC spAdminFundedDateee 
于 2014-06-19T14:10:47.743 回答
0

如果 a.funded_date 是 DATETIME 那么

a.funded_date between dateadd(day,-14,getdate()) and getdate()

如果 a.funded_date 是一个 DATE 那么

 a.funded_date between cast(dateadd(day,-14,getdate()) as date) and cast(getdate() as date)
于 2014-06-18T19:08:54.590 回答
0

为了检查 a.funded_date 是否在今天和两周前之间,您将需要几个 sql server 函数。第一个是GetDate()。这会将当前日期和时间作为日期时间值返回。

现在,您只想检查日期参数(而不是时间)。如果有人在下午 1 点运行您的存储过程,您不想从 14 天前的那一天下午 1 点之前删除所有数据。无论何时,您都想要从 14 天前开始的所有数据。为了解决这个问题,我们只想将 getDate() 更改为日期。所以,cast(getDate() as date)。今天,这将返回 6-18-14。

最后,您要检查两周前的日期。dateAdd允许您将指定的任何时间量添加到日期或时间。在这种情况下,您需要 14 天前的信息。这看起来像 dateadd(dd, -14, cast(getDate() as date))。

由于 between 是包容性的,你现在需要做的就是把它放在一起!

between dateadd(dd, -14, cast(getDate() as date)) and cast(getDate() as date)
于 2014-06-18T19:13:10.920 回答