Самые внешние nullif
функции в каждом случае, когда блок пытается преобразовать значение varchar в int, чтобы соответствовать их второму параметру. Вместо nullif(varcharValue, 0)
попробуйте использовать nullif(varcharValue, '0')
вместо этого, то есть:
create table dbo.Product
(
[ID] int not null,
[Product ID] int not null,
[Height UOM (Imperial)] varchar(2),
[Height (Imperial)] varchar(20),
[Height UOM (Metric)] varchar(2),
[Height (Metric)] varchar(20),
);
insert dbo.Product values
(1, 1, 'FT', '1.0', null, null),
(2, 2, 'IN', '2.0', null, null),
(3, 3, 'YD', '3.0', null, null),
(4, 4, null, null, 'MM', '4.0');
select
[Product ID],
[ID] as [PackRecordID],
case
when isnumeric([Height (Imperial)])=1 and ([Height (Metric)] is null or [Height (Metric)] = '') then
case
when [Height UOM (Imperial)]='FT' then nullif(try_cast((try_cast(isnull([Height (Imperial)],0) as numeric)*0.3048) as varchar(20)),'0')
when [Height UOM (Imperial)]='IN' then nullif(try_Cast((try_cast(isnull([Height (Imperial)],0) as numeric)*2.54) as varchar(20)),'0')
when [Height UOM (Imperial)]='YD' then nullif(try_Cast((try_cast(isnull([Height (Imperial)],0) as numeric)*0.9144) as varchar(20)),'0')
else nullif(try_cast((try_cast(isnull([Height (Metric)],0) as numeric)*1) as varchar(20)),'0') end
else nullif(try_cast((try_cast(isnull([Height (Metric)],0) as numeric)*1) as varchar(20)),'0')
end as [Height (Metric)],
case
when [Height UOM (Imperial)]='FT' and ([Height UOM (Metric)] is null or [Height UOM (Metric)] = '') then 'm'
when [Height UOM (Imperial)]='IN' and ([Height UOM (Metric)] is null or [Height UOM (Metric)] = '') then 'cm'
when [Height UOM (Imperial)]='YD' and ([Height UOM (Metric)] is null or [Height UOM (Metric)] = '') then 'm'
else try_cast([Height UOM (Metric)] as varchar(max))
end as [Height UOM (Metric)]
from dbo.Product;
, который возвращает:
Product ID PackRecordID Height (Metric) Height UOM (Metric)
1 1 0.3048 m
2 2 5.08 cm
3 3 2.7432 m
4 4 4 MM