SQL convert nvarchar into decimal dynamic rows - sql

I am not so familiar with using convert in SQL so thats why i am stuck with the following situation:
The column has a nvarchar(max) and i would like to convert it into decimal (18,2). But there are some rows that consist a "full" amount (see red box). For all values with the full amount i would like to have it as 1222,00
When creating a SQL view I got this error:
Error converting data type nvarchar to numeric.
How can i still convert this column into decimal?
Many thanks

From your pictures it seems to me like you are using SQL-Server. If so your problem is having , as decimal point instead of . It has nothing to do with having whole and decimal numbers in your data.
So you should replace it before converting data.
SELECT CONVERT(decimal(18,2), REPLACE(ColumnName,',','.')) FROM TableName
DB<>fiddle

Related

How to format a SSRS field with 2-digits of procession when the data is a mix of formats

I have data from a Stored Procedure that looks like this and needs to display on my report with 2-digits of precision. The data is all NVARCHAR but there is a mix of rows that looks numeric or says 'Max'.
How do I display only 2 digits of precision? I tried the following which works great on the numeric looking data but errors on 'Max'.
=FormatNumber(Fields!TBS.Value, 2)
Use an IIF to determine if the value is a number and IIF so, format it.
=IIF(ISNUMERIC(Fields!TBS.Value), FormatNumber(Fields!TBS.Value, 2), Fields!TBS.Value)
IsNumeric:
Returns a Boolean value indicating whether an expression can be
evaluated as a number.
MS SSRS Description

Hex values in SQL Database - converting to decimal

I am working with a SQL database that contains a column of numbers in hex. The numbers are entered into a third party application as a decimal and are saved in the SQL database as a hex value. Only issue is I can't figure out how the hex values correspond to the decimal values.
Example:
0x000000000032000000 is 50
0x0000000000E9030000 is 1001
0x00000000002C010000 is 300
Any help is much appreciated.
Try using a CONVERT
SELECT CONVERT(INT, 0xFFFFFF)

Convert nvarchar scientific notation value into number in SQL Server 2014

I have a column loaded into SQL Server with these nvarchar values:
ColumnName
==========
6.19e+014
.....
6.19e+014
Now, what would be the easiest way to convert this value into numbers again.
Kindly suggest. Thanks!
declare #String varchar(25)='6.19e+014'
Select cast(#String as float)
Returns 619000000000000
This will cast to FLOAT without issue:
SELECT CAST('6.19e+014' AS FLOAT)
If what you really want is the specific value that got converted to scientific notation in the first place, you'll have to go back in the process to before it got converted and fix that.
Also, try this
Select convert(numeric(15,0),ltrim(rtrim(str(column_name,15))))
from table_name

Nvarchar to Decimal and then sum it up

I have a nvarchar column and I need to convert it to Decimal. I tried the following code and it worked.
(CONVERT(decimal(2,2),CellContent,2))
Now when I tried to add a sum for the above code it is throwing me error
SUM (CONVERT(decimal(2,2),CellContent,2)) - Function Not recognised
How do I sum up the cell content field??
An arithmetic overflow error means SQL can't fit the nvarchar value in the number of positions you specified for the Decimal data type.
What values are you using for the CellContent column?
Try Decimal(12,2) maybe?
SUM (CONVERT(decimal(12,2),CellContent,2))

How do I Cast a string value formatted like "$400,000.88" to a decimal

I need to insert a string value that looks like money from one table to another table where the column type is decimal(18,2). I have tried code that looks like: CAST(REPLACE(REPLACE(d.Cost,',',''),'$','') AS DECIMAL(18,2)) AS 'Cost'
but get the error converting data type varchar to numeric.
It's not a good idea to store formatted values in your database, but this problem is easy to workaround.
The reason you can't cast it into Decimal is because currently it's a Varchar. So first cast it in Money and then you can cast it into something else.
This works in Sql Server:
select cast(cast('$400,000.88' as money) as decimal(10,2))
Try:
CAST(CAST(REPLACE(REPLACE(d.Cost,',',''),'$','') AS Float) AS DECIMAL(18,2)) AS 'Cost'
Thomas's comments made me rethink the work flow for the user. I was wanting the upload of excel files to be flexible but allowing to upload the 'cost' column as currency was causing the problem. They will be required to format the column in the excel file as number not currency.