Select all rows where a varchar column converts to a decimal - sql

I have a varchar column that has generally has a decimal value, but some times there is some garbage text characters in that field.
Is it possible to filter in the WHERE clause for rows that sucessfully convert to a decimal value?
I am using sql-server 2005

One way is the ISNUMERIC function:
select * from YourTable where ISNUMERIC(col1) = 1
There's one gotcha: isnumeric returns 1 whenever a string can be converted to any numeric type, including money. For example, say you have rows using varying decimal separators, like 7.9 and 7,9. Both will convert to money, and isnumeric returns 1 for both of them. But only one converts to decimal, depending on the SQL Server language settings.

Related

Need dynamic decimal places in SQL display

In a display column I'm getting values as follows:
12.000000
12.350000
13.230000
14.560000
I need to represent these values with dynamic decimal places upto 2 places i.e if there are zero's then it should ignore.
for example: 12.35 only , if its 12.500000 then it should display 12.5 only
float type removes trailing zeros, at least for SQL Server. Try casting your decimal to float.
SELECT CAST(12.350000 as float)
Returns
12,35
You could try the following (ANSI standard SQL):
SELECT TRIM(TRAILING '0' FROM TRIM(CAST(CAST(myvalue AS DECIMAL(12,2)) AS CHAR(30))))
FROM mytable;
I'm not sure what flavor of SQL you're using but the above should work (the extra TRIM() is on the off-chance that the result of the CAST() is padded with spaces.

How to retrieve the column with mandatory decimal point in Sql 2008?

I am retrieving a numeric value from one of the column in Sql. However I have to retrieve it by three decimal point (eg below).
Though if the value in the table is in the decimal point, then its giving the result as expected. But in case the value is not in decimal format and only an integer it must display .000
Below is the example.
Value in table= 2
Retrieval Value = 2
Required Value = 2.000
As per your requirement convert your incoming value in Decimal(18,3). like given below,
select convert(decimal(18,3),2)
To really show a value in a particular format, you can convert the value to a string. One method uses the str() function:
str(x, 18, 3)
Note that you can also cast() the decimal:
select cast(cast(x as decimal(18, 3)) as varchar(19))

SQL loses decimals after multiplication

I have a table which includes:
COUNT RISK
35 0.6456000000
11 0.5234000000
4 0.8431000000
I need a column to multiply the two columns. However I'm getting the result of:
TOTAL
35
11
4
COUNT - INT
RISK - VARCHAR
SQL is clearly rounding up the decimals as 1. I've tried casting as decimal, numeric and multiplying by 1.0. I need to retain the decimals for an actual calculation. Any help would be great
Convert result to decimal like this
SELECT
CONVERT(DECIMAL(16,10), COUNT * RISK) AS DecimalResult
FROM dbo.whatever;
Or convert COUNT to decimal
SELECT CAST(COUNT AS DECIMAL(16,10)) * RISK
This question is really suspicious. From the surface, it seems the two columns [Count] and [Risk] have different data types with [Count] as integer and [Risk] as decimal or float.
According to BOL, decimal/float data type has higher precedence, I will quote the BOL here
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type
So to me, in SQL Server, when you do
Select [Total]=[Count]*[Risk] from [your_table]
You cannot get the result as shown in the original question.

SQL - Convert number to decimal

I'm trying to convert a number to a decimal with two decimals places.
SELECT CONVERT(DECIMAL(10,2),12345)
The above would return 12345.00 but I'm trying to achieve 123.45
You need something like that:
SELECT CONVERT(DECIMAL(15,2),12345/100.0)
SELECT CONVERT(DECIMAL(10,2),CAST(12345 as float)/CAST(100 as float))
Correction: The premise is somewhat flawed, as the data type of a literal number without a decimal point is int, not numeric as implied by the question. In that case, you do need to convert the initial value to either numeric or decimal before dividing:
SELECT CONVERT(DECIMAL,12345)/100
or
SELECT CAST(12345 AS DECIMAL)/100
(cast is the SQL standard, so if you ever want to apply this to other databases, it would be the preferred method.)
Alternately, you can just add a decimal point to the divisor, as SQL server will return the more precise data type when doing arithmetic on heterogeneous types:
SELECT 12345/100.0
According to the documentation, the numeric data type is functionally equivalent to the decimal datatype, so there's really no reason to convert between the two. It seems that all you really want to do is divide the value you have by 100:
SELECT 12345/100

SQL Server 2008 - Mix of Int and Decimal Values stored as Varchar(50) Can't Convert To Decimal

I have two columns of data in a SQL Server 2008 table that I need to convert from varchar(50) over to decimal. Get different errors depending on the methods I try to use.
Error converting data type varchar to numeric.
Arithmetic overflow
Some ways I have tried:
CONVERT(DECIMAL(38,10),REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(value)),',',''),' ',''),'-',''))
CASE
WHEN value = 1
THEN CONVERT(numeric(14,2), value)
ELSE 'NA'
END
Here is a sample of the data in the table that I am trying to convert:
271.5
14.95
352.5
150
37.5
20.5
300
90
This shouldn't be this hard...
CAST should work well as long as the data is clean and the range is sufficient.
If you're not sure that all values are actually numeric, you can scrub them before converting them;
SELECT value, CASE WHEN ISNUMERIC(value)=1
THEN CAST(value AS NUMERIC(10,2))
END value2
FROM table1
This will convert the string value to a NUMERIC if it is actually convertible, and otherwise give the value NULL.
An SQLfiddle to test with.