あなたは、各WHEN
の値を持っている必要があり、かつELSE
有するべきである:FYI
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
:Len()
は文字列を返しませんが。
EDIT:コメントの一部に対処 SQL Serverの答えがあるかもしれない:SQLは意味がありません
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です:DATA_SOURCE、得意先、 AS CASE DATA_SOURCE = 'Test1を' とレン(CustomerNameの)= '35' その後DATA_SOURCE = 'Test2を' とサブ(CustomerNameの、1、35) 終了を選択します得意先名 からdbo.xx – user1368436
Data_Source CustomerID CustomerName Test xxx xxx PLC(LONDON BR Test1 xxx xxx PLC(LONDON BR2) – user1368436