Converting Varchar to negative decimal in brackets


Please login or register to vote for this query.

(click on this box to dismiss)

Startups Meta

Q&A for entrepreneurs faced with delivering a new product or service under conditions of significant uncertainty

declare @str as varchar(100) = '($421.24)';
declare @len as int;
select @len = len(@str);
declare @i as int;
set @i = 1;
declare @res as varchar(100) = '';
while(@i <= @len)
    declare @c as char(1);
    select @c = substring(@str, @i, 1);
    select @res+= case when ASCII(@c) = 46 or (ASCII(@c) between 48 and 57)
      then @c else '' end;

    set @i += 1;

select cast(@res as decimal(18, 2)) as [decimal_val];

Enter Parameters

Switch sites:
loading Hold tight while we fetch your results