Avatar of cipriano555
cipriano555
 asked on

data conversion problem

I'm trying to convert an nvarchar that holds a formatted numerical value into a FLOAT.

For example, we have  a table...

CREATE TABLE [dbo].[aadata_test](
      [testval] [nvarchar](50) NULL
) ON [PRIMARY]

it contains this value:


SELECT [testval]
  FROM [IntegratedMedicalBillingDataWarehouse].[dbo].[aadata_test]


testval
--------------------------------------------------
$1,000,000.00

(1 row(s) affected)

I want to convert it to a number, so I try this:

SELECT cast([testval] as float)
  FROM [IntegratedMedicalBillingDataWarehouse].[dbo].[aadata_test]

But get this error message.

Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.


How do you do this kind of conversion to get a numerical value?

Thanks,

C
Microsoft SQL Server 2008Microsoft SQL ServerMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
Patrick Matthews

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Patrick Matthews

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Patrick Matthews

BTW, CAST has no trouble converting your original value to data type money.  If you must have a float, you can then convert the money to float:

CAST(CAST([testvalu] AS money) AS float)
cipriano555

ASKER
Thank you for your help,  matthewspatrick.  

I do not understand the point of the money data type, why wouldn't you just use float?  Maybe this kind of conversion issue is the point?
Patrick Matthews

The money data type is useful for monetary transactions: it is capable of storing large numbers, and it is guaranteed to be precise to 4 decimal places.

Float is OK if you do not care too much about precision.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck