久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在另一個 Case 語句中使用 Case 語句的結(jié)果

Use result of Case statement in another Case statement(在另一個 Case 語句中使用 Case 語句的結(jié)果)
本文介紹了在另一個 Case 語句中使用 Case 語句的結(jié)果的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有很長的 SELECT 查詢,但我已將相關(guān)部分粘貼到此處.

I have quite a long SELECT query but I have pasted the relevant part here.

我需要將 CASE 語句的結(jié)果用于另一個 CASE 語句.我正在 SQL Server 中執(zhí)行此操作.

I need to use the result of the of my CASE statement to use in another CASE statement. I'm doing this in SQL Server.

非常感謝您的幫助.

SELECT
    CompanyContact.Name AS CompanyName,
    CASE 
       WHEN SUBSTRING(HeadLease.TenantBreakNotice, LEN(HeadLease.TenantBreakNotice), 1) = 'M'
          THEN CONVERT(VARCHAR(10), DATEADD(DD, -365 / (12 / SUBSTRING(HeadLease.TenantBreakNotice, 1, LEN(HeadLease.TenantBreakNotice) - 1)), HeadLease.TenantBreakDate), 103)
       WHEN SUBSTRING(HeadLease.TenantBreakNotice, LEN(HeadLease.TenantBreakNotice), 1) = 'Y'
          THEN CONVERT(VARCHAR(10), DATEADD(DD, -365 * (SUBSTRING(HeadLease.TenantBreakNotice, 1, LEN(HeadLease.TenantBreakNotice) - 1)), HeadLease.TenantBreakDate), 103)
       ELSE HeadLease.TenantBreakNotice
    END AS [TenantBreakNotice],  <-- I need this to be used in the case statement below.
    CASE
       WHEN [TenantBreakNotice] < CONVERT(varchar(10), getdate(), 103)  
          THEN 'Expiry' 
       WHEN [TenantBreakNotice] IS NULL 
          THEN 'Expiry' 
       ELSE 'Break' 
    END AS [LeaseEventType]
FROM 
    HeadLease  

推薦答案

不能在定義列別名的同一個 select 中使用列別名.通常的解決方案是重復(fù)邏輯(難以維護),使用子查詢或 CTE.SQL Server 提供了另一種優(yōu)雅的解決方案:

You cannot use a column alias in the same select where it is defined. The usual solution is to repeat the logic (hard to maintain), use a subquery, or CTE. SQL Server offers another elegant solution:

SELECT hl.Name AS CompanyName, v.TenantBreakNotice,
       (CASE WHEN v.TenantBreakNotice < CONVERT(varchar(10), getdate(), 103)  THEN 'Expiry' 
             WHEN TenantBreakNotice IS NULL THEN 'Expiry' 
             ELSE 'Break' 
        END) AS [LeaseEventType]

FROM HeadLease hl OUTER APPLY
     (VALUES (CASE WHEN SUBSTRING(hl.TenantBreakNotice, LEN(hl.TenantBreakNotice), 1) = 'M'
                   THEN CONVERT(VARCHAR(10), DATEADD(DAY, -365/(12/SUBSTRING(hl.TenantBreakNotice, 1, LEN(hl.TenantBreakNotice) -1)), hl.TenantBreakDate), 103)
                   WHEN SUBSTRING(hl.TenantBreakNotice, LEN(hl.TenantBreakNotice), 1) = 'Y'
                   THEN CONVERT(VARCHAR(10), DATEADD(DAY, -365*(SUBSTRING(hl.TenantBreakNotice,1, LEN(hl.TenantBreakNotice)-1)), hl.TenantBreakDate), 103)
                   ELSE hl.TenantBreakNotice
              END) v(TenantBreakNotice);

當(dāng)然,邏輯是不正確的,因為您將日期作為字符串進行比較.然而,這是你需要自己弄清楚的事情.不要將日期轉(zhuǎn)換為日期操作的字符串.并且,您應(yīng)該將結(jié)果輸出為 YYYY-MM-DD,以便格式明確.

Of course, the logic is incorrect, because you are comparing dates as strings. However, that is something you need to figure out yourself. Don't convert dates to strings for date operations. And, you should output the results as YYYY-MM-DD so the formats are unambiguous.

這篇關(guān)于在另一個 Case 語句中使用 Case 語句的結(jié)果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 中文字幕亚洲精品 | www97影院 | 欧美一区二区三区在线看 | 国产精品视频网 | 日韩综合网 | www.色综合| 91丨九色丨国产在线 | 欧美三级在线 | 九九热九九 | 欧洲视频一区二区 | 综合色婷婷 | 国产高清视频一区二区 | 久久久久成人精品免费播放动漫 | 久久看看 | 在线观看中文字幕视频 | 亚洲一区二区三区免费 | 黄色大全免费看 | 国产美女在线看 | 日韩二三区 | 成人区精品一区二区婷婷 | 91精品国产综合久久久久久首页 | 成人一区二区三区在线观看 | 偷拍自拍在线观看 | www.亚洲 | 一区二区国产精品 | 韩日一区二区 | 99国产精品视频免费观看一公开 | 日本淫视频 | 日韩欧美视频 | 91久久国产综合久久91精品网站 | 国产精品日韩欧美一区二区三区 | 国产九九九九 | 一区中文 | 久久高清国产 | 一级高清| 一区二区三区久久久 | 国产高清美女一级a毛片久久w | 久久99蜜桃综合影院免费观看 | 在线免费观看黄a | 国产成人综合在线 | 欧美区在线 |