久久久久久久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)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: h片在线看| 免费v片 | 欧美日韩黄色一级片 | 久视频在线观看 | 在线观看欧美一区 | 久久久久久久久久久久久九 | 亚洲高清一区二区三区 | 性福视频在线观看 | 91久久国产综合久久91精品网站 | 九九九视频在线观看 | 国产玖玖 | 欧美性生活网 | 懂色av色香蕉一区二区蜜桃 | 亚洲国产一区二区三区 | 天天操夜夜操 | 国产精品免费一区二区三区 | 亚洲视频在线观看一区二区三区 | 一区二区三区日本 | 日本一区二区在线视频 | 中文字幕免费中文 | 国产视频三级 | 亚洲日本免费 | 中文成人无字幕乱码精品 | 国产精品一区二区久久 | 成人日韩 | 美日韩一区二区 | 精品日韩 | 成人在线免费观看av | 久久久久久久久久久久91 | 久在线视频 | 欧美成人不卡 | 毛片免费看的 | 免费观看黄色一级片 | 一区二区三区视频 | 欧美成年黄网站色视频 | 99精品久久久国产一区二区三 | 久久久久久色 | 亚洲精品第一页 | 国产一区二区三区在线看 | 99久久精品国产一区二区三区 | 一区二区三区四区毛片 |