Trzeba mieć wartość dla każdego WHEN
, i powinien mieć 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()
nie zwraca ciąg znaków.
EDIT: SQL Server odpowiedź, która rozwiązuje niektóre komentarze mogą być:
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
że SQL nie ma sensu. Czy możesz podać przykładowe dane i pokazać, czego oczekiwałeś? –
To jest dokładnie sql: Wybierz DATA_SOURCE, CustomerID, CASE KIEDY DATA_SOURCE = 'test1' i Len (CustomerName) = '35' Następnie DATA_SOURCE = 'Test2' i podciąg (CustomerName, 1, 35) koniec, jak CustomerName Od dbo.xx – user1368436
Data_Source CustomerID CustomerName Test xxx xxx PLC, (LONDON BR Test1 xxx xxx PLC (LONDON BR2) – user1368436