你需要爲每個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()
不會返回一個字符串。
編輯: ,解決一些意見可能是一個SQL Server的答案:
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
即SQL沒有意義。你能舉出一些示例數據並展示你期望發生的事情嗎? –
這是確切的SQL:選擇DATA_SOURCE,客戶ID, CASE WHEN DATA_SOURCE = '測試1' 和Len(客戶名稱)= '35' 然後DATA_SOURCE = '的Test2' 和子串(客戶名稱,1,35) 完AS客戶姓名 來自dbo.xx – user1368436
Data_Source客戶ID客戶名稱測試xxx xxx PLC,(倫敦BR測試1 xxx xxx PLC(倫敦BR2) – user1368436