2017-03-31 87 views
0

假設我有序列號,測試名稱和其他幾個欄目,我想寫一個條件,如果TESTNAME is null特定序列號,然後設置TESTNAME空白否則執行內部聯接如果MSSQL else條件

SELECT 
    (A.PTNUMBER + '-' +A.SL_NO) AS ENUMBER, 
    D.ENGINEER AS REQ, D.DATETIME as "DATE", 
    (select Value 
    from DROPDOWN 
    where B.TEST_NAME=CONVERT(VARCHAR,DropdownID)) TESTNAME, 
    TABLE_NAME AS TABLETD 
FROM INSPECTION D 
INNER JOIN TABLEA A ON D.ENGID = CONVERT(VARCHAR,A.EN_ID) 
INNER JOIN TABLEB B ON B.ENGID = CONVERT(VARCHAR,A.EN_ID) 
INNER JOIN TABLEC C ON C.ENGID = CONVERT(VARCHAR,A.EN_ID) 
+1

結帳的情況下的關鍵字(https://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql)向我們展示你到目前爲止所擁有的。我們不介意讀者:) – Jeremy

+0

下一次,從查詢開始。它會阻止無法捕捉問題範圍或正確解決問題的答案。假設性問題不是[最小,完整或可驗證示例](https://stackoverflow.com/help/mcve)。爲了改進您的答案,[請查看此主題](https://stackoverflow.com/help/how-to-ask)幷包含測試數據[如此處所述](https://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum /)以及預期結果。乾杯 – scsimon

回答

0

不知道你組測試名的意思爲空白但如果你的意思是使用一個SELECT查詢,那麼你可以不喜歡

select *, 
case when TESTNAME is null and serial_number = some_value then '' end as TESTNAME 
from mytable 
+0

當B.TEST_NAME爲空或B.TEST_NAME ='0',然後'' ,而不是設置TEST_NAME空白,我想設置表D中的另一列'VALUES'到BLANK自 在表D我有一個列(dropdownID)映射到表B中的列(test_name)。 因此,我的目標是當TEST_NAME(在表b中)爲0或null時,將值(在表D中)設置爲空。 我試過的情況下,當TEST.TEST_NAME IS NULL或TEST.TEST_NAME ='0',然後D.Values ='',但它說不正確的語法。請幫助我。 謝謝。 – Reddy

+0

SELECT(A.PTNUMBER +' - '+ A.SL_NO)AS ENUMBER,D.ENGINEER AS REQ,D.DATETIME as「DATE」,SerialNo = xxx且B.TEST_NAME爲空或B的情況。TEST_NAME ='0'then''else(select from DROPDOWN where B.TEST_NAME = CONVERT(VARCHAR,DropdownID))end AS VALUES,TABLE_NAME AS TABLETD FROM INSPECTION D INNER JOIN TABLEA A ON A.ENGID = CONVERT(VARCHAR, A.EN_ID)INNER JOIN TABLEB B ON B.ENGID = CONVERT(VARCHAR,A.EN_ID) INNER JOIN TABLEC C ON C.ENGID = CONVERT(VARCHAR,A.EN_ID) INNER JOIN TABLED D ON D.ENGID = CONVERT (VARCHAR,A.EN_ID); – Reddy

0

您可以結合一個case表達式和coalesce()以及您的連接來選擇要返回的值。

select serial_number, ... 
    ,case when coalesce(testname,'') <> '' 
      then t2.testname 
     else coalesce(testname,'') end 
from t 
    inner join t2 
    on ... 

您可以使用isnull()coalesce()在SQL Server返回不同的值來代替null

select isnull(testname,'') 

select coalesce(testname,'') 

這兩者之間的主要區別是,coalesce()可以支持多於2個參數,它選擇第一一個不是null。兩者之間更多的差異answered here.

select coalesce(testname,testname2,'') 

coalesce()也是標準ANSI SQL,所以你會在大多數RDBMS找到它。 isnull()是特定於SQL Server的。

參考:

+0

以下是我的查詢,如何將TESTNAME設置爲空如果它的NULL並執行連接條件並填充值SELECT(A.PTNUMBER +' - '+ A.SL_NO)AS ENUMBER,D.ENGINEER AS REQ, D.DATETIME作爲「DATE」,(從DROPDOWN中選擇值,其中B.TEST_NAME = CONVERT(VARCHAR,DropdownID))TESTNAME, TABLE_NAME AS TABLETD FROM INSPECTION D INNER JOIN TABLEA ON ON D.ENGID = CONVERT(VARCHAR,A.EN_ID) INNER JOIN TABLEB B ON B.ENGID = CONVERT(VARCHAR,A.EN_ID) INNER JOIN TABLEC C ON C.ENGID = CONVERT(VARCHAR,A.EN_ID) – Reddy

0
SELECT (A.PTNUMBER + '-' + A.SL_NO) AS ENUMBER, 
     D.ENGINEER AS REQ, 
     D.DATETIME as "DATE", 
     case 
     when SerialNo = xxx and TESTNAME is null then '' 
     else (select Value from DROPDOWN where B.TEST_NAME = CONVERT(VARCHAR, DropdownID)) 
     end AS TESTNAME, 
     TABLE_NAME AS TABLETD 
    FROM INSPECTION D 
    INNER JOIN TABLEA A ON D.ENGID = CONVERT(VARCHAR, A.EN_ID) 
    INNER JOIN TABLEB B ON B.ENGID = CONVERT(VARCHAR, A.EN_ID) 
    INNER JOIN TABLEC C ON C.ENGID = CONVERT(VARCHAR, A.EN_ID); 
+0

我想測試一個TEST_NAME是否爲null,然後設置爲0爲空,但由於TEST_NAME是一個varchar,我的SQL語句不會將null設置爲空白,您能幫我解決這個問題嗎? – Reddy

+0

這是條件TEST.TEST_NAME IS NULL OR TEST.TEST_NAME ='0'然後'' – Reddy

+0

我不明白你需要什麼,對不起。 SELECT(A.PTNUMBER + ' - ' + A.SL_NO)AS ENUMBER, D.ENGINEER AS REQ, D.DATETIME爲 「DATE」, 情況 時的SerialNo = xxx和測試名是空值,則 '' 否則(從下拉式鑄造(價值爲varchar)其中B.TEST_NAME = CONVERT(VARCHAR,DropdownID)) 端AS測試名, TABLE_NAME AS TABLETD 驗d INNER JOIN TABLEA A ON D.ENGID = CONVERT(VARCHAR,A. EN_ID) INNER JOIN TABLEB B ON B.ENGID = CONVERT(VARCHAR,A.EN_ID) INNER JOIN TABLEC C ON C.ENGID = CONVERT(VARCHAR,A.EN_ID); – IngoB