convert sql column from nvarchar to numeric

I have a SQL database called BBKTrial, a table called dbo.sheet1$. a column called [as-am].  This column is nvarchar(225) format.  I need to convert it to numeric.

How can I do this??
Curtis LongAsked:
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.

PortletPaulEE Topic AdvisorCommented:
see CAST() or CONVERT()

Conversion in that direction (string >>> to >>> number) will fail is any non-numeric data is in that column

either of these will do it.

CONVERT( [as-am] as decimal(12,3) )

or

CAST( [as-am] , decimal(12,3) )

you can change the numeric type to suit whatever it is you need.

BUT. this conversion can cause failures.

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
PortletPaulEE Topic AdvisorCommented:
You can protect against conversion errors using iSNUMERIC() but this completely safe.

e.g.

select CONVERT( [as-am] as decimal(12,3) )
from that_table
where ISNUMERIC([as-am]) = 1

a result of 1 from ISNUMERIC() indicates the parameters holds numeric information but this can include items such as currency symbols

We could advise better if you supplied this:

select distinct [as-am]
from that_table
where ISNUMERIC([as-am]) = 0
Jim HornMicrosoft SQL Server Data DudeCommented:
The above works for a query. If you need to change the column data type in the table to numeric, then...
ALTER TABLE dbo.sheet1$
ALTER COLUMN as-am numeric(19,4)  
-- change the 19 and 4 to whatever percision and scale you want

Open in new window


BUT this will only work if all values can be converted to numeric in the first place.  Use Paul's ISNUMERIC() function to test.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Dung DinhDBA and Business Intelligence DeveloperCommented:
Another way, you can use below query

SELECT
CASE WHEN ISNUMERIC([as-am]) =1 THEN CONVERT(numeric,[as-am]) ELSE 0 END
FROM <your table>
Vitor MontalvãoMSSQL Senior EngineerCommented:
The Experts above already told you how to convert so I'll focus in another thing in your question.

You said: "a table called dbo.sheet1$. a column called [as-am].  This column is nvarchar(225) format."
This seems to me a table exported from an Excel spreadsheet using the Import/Export wizard. When you export data by the wizard, you can define the datatype of the destination column, so next time you can do it immediatly during the Import/Export process so no convertion will be needed after the Export.
Curtis LongAuthor Commented:
How do you define the data type during import??  I do not see that option in the wizard
Vitor MontalvãoMSSQL Senior EngineerCommented:
In the Import Wizard when you chose the worksheets and the destination table, click on "Edit Mappings..." button and will pop-up a new screen where you can change column names and data types.

EditMappings.PNG
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.