Convert Varchar with Exponent to Decimal Data type - sql

I have column Amount with 10,000 records and its look like this.
1.24E4
27.27E1
3.25E2
and etc.
Now I need to update the column to make this result
12400
272.7
325
I need to know how to convert and update the column from varchar to decimal.
Take note that my Column Amount is in Varchar Data type.

select convert(float, '1.24E4')
select convert(float, '27.27E1')
select convert(float, '3.25E2')

Simply update with a cast to FLOAT:
UPDATE YOUR_TABLE
SET YOUR_COLUMN = CAST(YOUR_COLUMN AS FLOAT)
WHERE....your condition if there

Related

SQL Server Management Studio error converting a string that does not exist

I have a table with a varchar column that has any kind of values. I wanted to filter only numeric values and than compare them with other Numbers. But I always get:
Error converting varchar value "!=")" to the int-datatyp
But !=") does not even exist in there
At first I thought it might be in there and just check every requirement even if the numeric check already failed. So I put it in a sub query. But the error is still happens.
This is my query:
SELECT CAST(data AS float)
FROM
(SELECT DISTINCT Data
FROM [dbo].[log]
WHERE [Time]>='2021-08-04 00:00:00.000'
AND ISNUMERIC(data) = 1
AND Data NOT LIKE '%[^(0-9\-\.)]%'
) AS t
WHERE
data > 50
Any ideas?
You could use TRY_CAST to get NULL for invalid input data - instead of an exception. ISNUMERIC is notoriously bad at telling whether a column value is really numeric - or not.....
SELECT
NumericData
FROM
(SELECT DISTINCT
Data,
NumericData = TRY_CAST(data AS float)
FROM
[dbo].[log]
WHERE
[Time]>='2021-08-04 00:00:00.000'
AND Data NOT LIKE '%[^(0-9\-\.)]%') AS t
WHERE
t.NumericData > 50
and you could find the offending data by using:
SELECT
data,
NumericData = TRY_CAST(data AS float)
FROM
(SELECT DISTINCT Data
FROM [dbo].[log]
WHERE [Time]>='2021-08-04 00:00:00.000'
AND Data NOT LIKE '%[^(0-9\-\.)]%') AS t
WHERE
TRY_CAST(data AS float) IS NULL
TRY_CAST() (or a similar function is definitely the right approach. I want to point out that you can simplify your query:
SELECT DISTINCT TRY_CAST(data AS float)
FROM [dbo].[log]
WHERE [Time] >= '2021-08-04' AND
TRY_CAST(data AS float) > 50;
You do not need the time component for the time comparison. You don't need to use LIKE, because TRY_CAST() does that for you.

How to convert to decimal when data is number but not convert when data is text?

I am using SQL Server 2008 r2.
There are 31 fields (D1-D31) as varchar and keep 2 format data as number and text such as 'SD' and '123.456'.
I use the command for Data Base Expert of Crystal Report like this.
select
case
when D1 in('SD') then d1
when D1 is NULL then ''
else convert(decimal(7,2),d1)
end
.
.
.
case D2-D31
from Rec2019 where ID ='P0009'
It's show Error converting data type varchar to numeric.
How to fix it ? please..
A column has one datatype so you cannot place DECIMAL and VARCHAR values inside same column. I think you want to round numeric-ish values. In that case you can use the following:
CASE
WHEN ISNUMERIC(d1) = 1 AND d1 NOT IN ('+', '-', '$') THEN CAST(CAST(d1 AS DECIMAL(7, 2)) AS VARCHAR(9))
WHEN d1 IS NULL THEN ''
ELSE d1
END
Demo on db<>fiddle
For SQL Server 2008, you can check using ISNUMERIC, where as SQL Server 2012+ have TRY_CONVERT or TRY_CAST to make it simple like following.
TRY_CONVERT(decimal(7,2), D1)
Since ISNUMERIC had some small downsides, for instance with scientific notation of numbers, I would use TRY_CAST, like this: (note: SQLServer 2012+)
CREATE TABLE #T (ColA VARCHAR (10))
INSERT INTO #T VALUES ('1'), ('A'), ('2');
SELECT *, TRY_CAST (ColA AS DECIMAL (4,2)) AS Numb
FROM #T
Sidenote: One column has one data-type, you cannot combine datatypes in one column in SQL Server.
To convert when the data is number, and not convert when the data is text, you can check the type with the isnumeric function.
select
case when isnumeric(D1) then convert(decimal(7,2), D1) else null end as D1_asnumeric
from XXX

SQL Select range for nvarchar column

How do I select from tables where the column_a in range 55 - 42000.
Note : column_a's datatype is in nvarchar
I have try like this, but no luck
SELECT
SoftwareName, SoftwareImageTeaser, SoftwarePrice, SoftwareDescription
FROM
View_Software_Listing_With_Category
WHERE
(SoftwarePrice >= '55' AND SoftwarePrice <= '42000')
SELECT SoftwareName,SoftwareImageTeaser,SoftwarePrice,SoftwareDescription
FROM View_Software_Listing_With_Category
WHERE (CAST(CAST(SoftwarePrice AS FLOAT) AS INT) BETWEEN 55 AND 42000
Try this,As your SoftwarePrice column is nvarchar, you need to convert it in Integer for comparing with integer values. Then you can use between clause for filtering records.
SELECT SoftwareName,SoftwareImageTeaser,SoftwarePrice,SoftwareDescription
FROM View_Software_Listing_With_Category
WHERE Cast(SoftwarePrice as Int) between 55 AND 42000

convert varchar(8) values and use ASC in T-SQL

I have a data in the table in the form below and it is in varchar(8) datatype
Total
100
101
104.5
88
1038
64
108.3
10872
900
I like to use ASC in T-sql so that I can display it into ascending order, however I
cannot do it as it is in varchar(8) form
For example
select Total from
Table A
Order by Total ASC
How to first add these values into Temporary temp table?
and How to convert this varchar(8) values and into what? so that
you can display them in ASC or ascending order using T-SQL query?
Anyone?
You could cast the value like this.
SELECT
Total
FROM Table A
ORDER BY CAST(Total AS FLOAT) ASC
You may lose data converting back to float.
So here is a varchar based sort.
DECLARE #badDesign TABLE (floatcol varchar(8) NOT NULL);
INSERT #badDesign VALUES ('100'),('101'),('104.5'),('88'),('1038'),('64'),('108.3'),('10872'),('900'),('108'), ('108.32'), ('108.4')
SELECT *
FROM #badDesign
ORDER BY
RIGHT('00000000' +
CASE
WHEN CHARINDEX('.', floatcol) = 0 THEN floatcol
ELSE LEFT(floatcol, CHARINDEX('.', floatcol)-1)
END
, 8),
CASE
WHEN CHARINDEX('.', floatcol) = 0 THEN '.0'
ELSE SUBSTRING(floatcol, CHARINDEX('.', floatcol)+1, 8)
END
The values from your example looks like float numbers. So
1) Since they all have no more than 8 digits, you can cast it to float(53) (it has about 15 decimal digits precision) without loss of data. Or to decimal(15,7) to be completely sure.
2) Generally it's strange to store float values as strings in the database.
Use CAST or CONVERT functions, e.g.:
select Total from
Table A
Order by CAST(Total as float) ASC
Reference: http://msdn.microsoft.com/en-us/library/ms187928.aspx
Unless you want to keep the converted values, you don't need to store it in a temporary table. Just convert them for the sorting:
select Total
from [Table A]
order by cast(Total as float)
(Ascending is the default way to sort, so you don't have to specify that.)

Sql query to convert nvarchar to int

I have to query for total amount of a column using an aggregate function. The column data type is NVARCHAR(MAX). How can I convert it to Integer?
I have tried this:
SELECT SUM(CAST(amount AS INT)),
branch
FROM tblproducts
WHERE id = 4
GROUP BY branch
...but I'm getting:
Conversion failed when converting the nvarchar value '3600.00' to data type int.
3600.00 is not integer so CAST via float first
sum(CAST(CAST(amount AS float) AS INT))
Edit:
Why float?
no idea of precision or scale across all rows: float is the lesser evil perhaps
empty string will cast to zero for float, fails on decimal
float accepts stuff like 5E-02, fails on decimal
In addition to gbn's answer, you need to protect against non-numeric cases:
sum(CASE WHEN ISNUMERIC(Amount)=1 THEN CAST(CAST(amount AS float) AS INT)END )
SELECT sum(Try_Parse(amount as Int Using 'en-US')),
branch
FROM tblproducts
WHERE id = 4
GROUP BY branch