Usted necesita tener un valor para cada WHEN
, y debe tener un 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()
no devuelve una cadena.
EDIT: respuesta Servidor A SQL que aborda algunos de los comentarios pueden ser:
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
que SQL no tiene sentido. ¿Puedes dar algunos datos de ejemplo y mostrar lo que esperabas que sucediera? –
Este es el sql exacta: Seleccionar data_source, CustomerID, CASE CUANDO data_source = 'Test1' y Len (CustomerName) = '35' Entonces data_source = 'Test2' y subcadena (CustomerName, 1, 35) End AS CustomerName De dbo.xx – user1368436
Data_Source CustomerID CustomerName Test xxx xxx PLC, (LONDON BR Test1 xxx xxx PLC (LONDON BR2) – user1368436