आप प्रत्येक WHEN
के लिए एक मूल्य की आवश्यकता है, और एक ELSE
है चाहिए:
Select Data_Source, CustomerID,
CASE
WHEN Data_Source = 'Test1' and Len(CustomerName) = 35 THEN 'First Value'
WHEN Data_Source = 'Test2' THEN substring(CustomerName, 1, 35)
ELSE 'Sorry, no match.'
END AS CustomerName
From dbo.xx
FYI करें: Len()
एक स्ट्रिंग वापस नहीं करता है।
संपादित करें: एक एसक्यूएल सर्वर जवाब यह है कि कुछ टिप्पणियों को हो सकता है पते:
declare @DataSource as Table (Id Int Identity, CustomerName VarChar(64))
declare @VariantDataSource as Table (Id Int Identity, CostumerName VarChar(64))
insert into @DataSource (CustomerName) values ('Alice B.'), ('Bob C.'), ('Charles D.')
insert into @VariantDataSource (CostumerName) values ('Blush'), ('Dye'), ('Pancake Base')
select *,
-- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs are not handled gracefully.
Substring(CostumerName + Replicate('.', Len(CustomerName)), 1, Len(CustomerName)) as Clustermere,
-- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs in CustomerName are explicitly handled.
case
when CustomerName is NULL then ''
when Len(CustomerName) > Len(CostumerName) then Substring(CostumerName, 1, Len(CustomerName))
else Substring(CostumerName + Replicate('.', Len(CustomerName)), 1, Len(CustomerName))
end as 'Crustymore'
from @DataSource as DS inner join
@VariantDataSource as VDS on VDS.Id = DS.Id
एसक्यूएल मतलब नहीं है यही कारण है कि। क्या आप कुछ उदाहरण डेटा दे सकते हैं और दिखा सकते हैं कि आप क्या होने की उम्मीद करते हैं? –
यह सटीक एसक्यूएल है: चयन DATA_SOURCE, CustomerID, मामला जब DATA_SOURCE = 'Test1' और लेन (CUSTOMERNAME) = '35' फिर DATA_SOURCE = 'Test2' और सबस्ट्रिंग (CUSTOMERNAME, 1, 35) अंत के रूप में ग्राहक नाम dbo.xx – user1368436
डेटा_Source ग्राहक आईडी ग्राहकनाम परीक्षण xxx xxx पीएलसी, (लंदन बीआर टेस्ट 1 xxx xxx पीएलसी (लंदन बीआर 2) – user1368436