case语句在where条件中

时间:2017-11-09 11:19:05

标签: sql-server

如何在where条件中使用Case语句来检查Date是否大于其他日期(如果是这样的话)i从我的函数计算一个日期然后检查今天的日期并需要设置日期where子句。

select L.FDGL as [FDGL],L.brn as [BRANCH Name], L.LoyaltyNo as [LoyaltyNo],
    L.Loan_no as [Loan No],L.Client_name as [Customer Name], convert(datetime,L.Loan_Dt,103) as [Loan Disburse Date],
    convert(datetime,L.Loan_DueDt,103) as [Due Date], convert(datetime,DATEADD(day, MFL_BL.[dbo].get_NPADAYS(U.[State]),(L.Loan_lsint+1)),103) as [NPA DATE]
from Loans L
    inner join users U on U.FDGL=L.FDGL
where Loan_stat='A'
    and (case when (L.Loan_lsint > L.Loan_DueDt) then getdate() else 0 end ) end )

3 个答案:

答案 0 :(得分:1)

实际的完整查询似乎隐藏在评论中,这就是完整查询:

之前:

SELECT
      l.FDGL AS [fdgl]
    , l.brn AS [branch name]
    , l.LoyaltyNo AS [loyalty no]
    , l.Loan_no AS [loan no]
    , l.Client_name AS [customer name]
    , CONVERT(datetime, l.Loan_Dt, 103) AS [loan disburse date]
    , CONVERT(datetime, l.Loan_DueDt, 103) AS [due date]
    , CONVERT(datetime, DATEADD(DAY, MFL_BL.[dbo].get_NPADAYS(u.[State]), (l.Loan_lsint + 1)), 103) AS [npa date]
FROM Loans l
INNER JOIN users u ON u.FDGL = l.FDGL
where Loan_stat='A' 
and case when (L.Loan_lsint > L.Loan_DueDt) 
         then DATEADD(day,MFL_BL.[dbo].get_NPADAYS(U.[state]),L.Loan_DueDt‌​) <=getdate() 
    end
  1. 您不会将case expression与任何内容进行比较。在and之后应该有一些&#34;列=&#34;或类似,然后是案例表达。
  2. 目前的case expression目前无效或完整。你试图比较一个函数与getdate()之后的then 不能做:&#39;然后`应该只返回一个值,而不是比较2个值。
  3. 在select子句中转换为varchar更为常规,例如convert(varchar(10),...,103)用于在DD / MM / YYYY中进行演示
  4. SELECT
          l.FDGL AS [fdgl]
        , l.brn AS [branch name]
        , l.LoyaltyNo AS [loyalty no]
        , l.Loan_no AS [loan no]
        , l.Client_name AS [customer name]
        , CONVERT(varchar(10), l.Loan_Dt, 103) AS [loan disburse date]
        , CONVERT(varchar(10), l.Loan_DueDt, 103) AS [due date]
        , CONVERT(varchar(10), DATEADD(DAY, MFL_BL.[dbo].get_NPADAYS(u.[State]), (l.Loan_lsint + 1)), 103) AS [npa date]
    FROM Loans l
    INNER JOIN users u ON u.FDGL = l.FDGL
    WHERE Loan_stat = 'A'
    AND (
         l.Loan_lsint > l.Loan_DueDt
       OR 
         DATEADD(DAY, MFL_BL.[dbo].get_NPADAYS(u.[state]), l.Loan_DueDt) <= GETDATE()
        )
    

    布尔逻辑可以替换case expression。上面显示的逻辑是对您的实际意图的最佳猜测,但至少语法是有效的。

    依赖于where子句中的函数调用仍然是不好的做法,因为它会导致性能不佳。

    ----

      

    如果l.Loan_lsint&gt; l.Loan_Duedt然后在where子句中我使用l.Loan_Duedt   或者l.Loan_Lsint日期

    WHERE  Loan_stat = 'A'
    AND (
              (
               l.Loan_lsint > l.Loan_DueDt
              AND
               DATEADD(DAY, MFL_BL.[dbo].get_NPADAYS(u.[state]), l.Loan_DueDt) <= GETDATE()
              )
        OR
              (
               l.Loan_lsint <= l.Loan_DueDt
              AND
               DATEADD(DAY, MFL_BL.[dbo].get_NPADAYS(u.[state]), l.Loan_Lsint) <= GETDATE()
              )
        )
    

答案 1 :(得分:-1)

你可以做这样的事情

    CREATE TABLE #tmp (id int, date1 DATETIME, date2 DATETIME, date3 DATETIME)

    insert into #tmp values(1,'2017-01-01','2017-02-02',null)
    insert into #tmp values(2,'2017-05-01','2017-04-02','2017-11-09')

    SELECT * FROM #tmp
    WHERE date3 = CASE WHEN date1> date2
            THEN CONVERT(DATE,GETDATE())
         END

    DROP TABLE #tmp

答案 2 :(得分:-1)

where Loan_stat='A' and [your column name]=(

case 
    when (L.Loan_lsint > L.Loan_DueDt) then getdate() else [Date] end ) 
end )

CASE语句将返回与[your column name]匹配的值。 如果语句第一部分返回日期Data但第二部分返回int类型。 我认为它会解决你的问题。