Replace % with empty string in text field and convert to numeric

I'm importing data from a tab delimited file into SQL Server with Bulk Insert.

Two of the fields are percentages formatted as "100%" or "95.83%", ..., but I have to import those columns into my temp table as nvarchar().

However, when I push this data into my production table, I want to do so as a numeric value.  What syntax can I use to convert this text value to numeric values like 1.0000 and 0.9583?

Is there a better syntax than:

Cast(LEFT(Dashboard_SSQ_Score_Percentage, LEN(Dashboard_SSQ_Score_Percentage)-1) as float)/100 as SSQ_Pct
LVL 51
Dale FyeOwner, Developing Solutions LLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Looks like a winner as long as you can make an assumption that the last character will always be the % sign, and there are no values 1000% or greater that have thousands separators.

CREATE TABLE #tmp (val_in NVARCHAR(10), val_out DECIMAL(19,4))  

INSERT INTO #tmp (val_in)
VALUES ('100%'), ('95.83%')

UPDATE #tmp
SET val_out = Cast(LEFT(val_in, LEN(val_in)-1) as float)/100

SELECT * FROM #tmp

Open in new window

val_in.jpg
0
Shaun KlineLead Software EngineerCommented:
You could use REPLACE as an option to get rid of the percent sign as well.

DECLARE @Test AS NVARCHAR(10)

SET @Test = '100%'

SELECT CAST(REPLACE(@Test, '%', '') AS DECIMAL(9,4)) / 100

SET @Test = '95.86%'

SELECT CAST(REPLACE(@Test, '%', '') AS DECIMAL(9,4)) / 100

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Thanks, guys.

I absolutely hate writing code which demands that a particular character be in a specific place in a string, especially when I'm getting the data from an outside source.  I really wanted to go with Replace, but incorrectly assumed that the % would be viewed as a wildcard rather than a literal, so I tried it with '[%]' and that did not work; I didn't even try it without the brackets.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.