lefeal lefeal - 1 month ago 6
SQL Question

fixing sql data EG 000-575.33

I was given a few thousand records of varchars such as the one above. They all need to be 10 characters, and I guess they just indiscriminately padded zeros on them. How can I move the negative sign over to the left so

000-575.33
becomes
-000575.33
?

Answer

Something like this should do. If they aren't already padded correctly let us know and we can stuff those.

declare @v varchar(64) = '000-575.33'
select 
    case 
        when CHARINDEX('-',@v) > 1 then '-' + replace(@v,'-','')
        else @v
    end
Comments