convert varchar '100%' to float '1.00'

  • I dont know why this seems to be so difficult. I have searched all over the web and these forums, yet no one seems to have dealt with my current situation.

    I have a varchar column with values: 100%, 50%, 30%, etc. I am trying to convert these to float: 1.00, 0.50, 0.30, etc. When I try to cast or convert I get an error because of the '%'. When I try to replace to '%' with ' ' , I get an error. This seems so silly, yet I can't figure it out.

  • I can't reproduce your problem. Or maybe you're doing something different.

    CREATE TABLE Test(

    string_pct varchar(10))

    INSERT INTO Test

    VALUES

    ('100%'),

    ('50%'),

    ('30%'),

    ('10%')

    SELECT string_pct,

    CAST( REPLACE(string_pct, '%', '') AS float) / 100

    FROM Test

    GO

    DROP TABLE Test

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • wonderful, that fixed my problem. Im not sure why when I tried the following it failed. Again, thans a bunch

    replace(string_pct, '%', ' ')

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply