Hopefully this is quite straight forward. I am trying to convert a column to int so I can sum on it.
SELECT (cast(RTRIM(pricing_prices)as int)) FROM returnxx where RMAid= '5'
errors
Conversion failed when converting the nvarchar value
'9.8000000000000007' to data type int.
If I try bigint I get another error
Error converting data type nvarchar to bigint.
First convert into Numeric and then INT.
Try this:
DECLARE #Var varchar(100)='9.8000000000000007'
SELECT CAST(CAST(#Var AS NUMERIC(35,16)) AS INT)
Result:
9
If you want ROUND value, just convert into Numeric:
SELECT CAST(#Var AS NUMERIC)
Result:
10
If you want DECIMAL value, just convert into Numeric:
SELECT CAST(#Var AS NUMERIC(8,2))
Result:
9.80
I have this problem in the past. i was able to resolve it by casting the column to decimal first then cast it to an Integer. You can cast it to any decimal of your choice
DECLARE #pricing_prices NVARCHAR(20)='9.8000000000000007' AS pricing_prices
SELECT CAST(CAST(#pricing_prices AS DECIMAL(9,2)) AS INT)
Output
pricing_prices
9
Related
I have a column in database table defined as varchar(1000) null
In one of the situations, the data is entered as N/A - so I can force to ignore all values and use null.
But there are some values which I want to use.
If I use cast, convert, try_cast, tryconvert, to varchar all fails, but if I do it to int, it works, but the try cast, try convert works.
I am not sure why it is failing when I use try_cast and it says:
convert failed to convert N/A value to int.
Even I am not converting it to INT
Try the following:
DECLARE #test_val AS varchar(100) = 'N/A'
SELECT TRY_CAST(#test_val AS int)
This gives a result of NULL
DECLARE #test_val AS varchar(100) = '3'
SELECT TRY_CAST(#test_val AS int)
Gives a result of 3.
It may be that your SQL has a syntax error.
I am trying to convert a float in the format yyyymmdd to datetime. According to this the correct style code for that format is 112.
Code:
select
convert(datetime,cast(myDate as numeric),112)
from MyTable
Error:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
I get the same error without the cast as numeric part. I've been looking around for a couple hours, but I haven't been able to find anything that fixes this. If you know a better way to convert the float to a datetime I would be open to that idea.
Thank you for your help.
EDIT
Here is the working code:
SELECT
case when isdate(CAST(CAST(myDate AS INT) AS VARCHAR(8))) = 1
then CAST(CAST(CAST(myDate AS INT) AS VARCHAR(8)) AS DATETIME)
end
from MyTable
I wrapped it in the isdate because there were a few invalid dates in there. Thanks to Matt for the help.
EDIT2
Better version:
SELECT
TRY_CAST(CAST(CAST(myDate AS INT) AS VARCHAR(8)) AS DATETIME)
FROM MyTable
First you must convert the FLOAT to a VARCHAR. And since FLOAT has a number of decimal points, it must first be converted to an INT.
DECLARE #myDate FLOAT
SET #myDate = 20140721
SELECT CAST(CAST(#myDate AS INT) AS VARCHAR(8))
--20140721
Then you can convert the VARCHAR to DATE or DATETIME format.
DECLARE #myDate FLOAT
SET #myDate = 20140721
SELECT CAST(CAST(CAST(#myDate AS INT) AS VARCHAR(8)) AS DATE)
--2014-07-21
How can I safely convert the following:
ISNULL(t1.UserPercentage,0) AS UserPercentage
Current the t1.UserPecentage column is a decimal(9,2), I want to convert it to an integer.
Since the value can be NULL, the conversion should be done based on the result of the call to ISNULL correct?
DECLARE #Var DECIMAL(9,2) = 2.67
SELECT CAST(ROUND(#Var,0) AS INT)
This query will keep nulls but If you want to convert NULLS into 0
THEN
SELECT CAST(ROUND(ISNULL(#Var, 0),0) AS INT)
Note
Converting DECIMAL to int without ROUNDing it will give you a bit less
accurate result then if you ROUND it to zero before you cast it as
int.
Direct conversion into INT will simply truncate any decimals but if
you ROUND it to zero 1st and then CAST as int , it will give you more
realistic results.
Example
DECLARE #Var DECIMAL(9,2) = 2.67
SELECT CAST(#Var AS INT)
This will return 2, but basic rules of mathematics says this value is 3 if it is rounded to a whole number.
DECLARE #Var DECIMAL(9,2) = 2.67
SELECT CAST(ROUND(#Var,0) AS INT)
This query will return 3. More accurate then just converting it to INT.
If your desired result is a simple truncation of the decimal points, using FLOOR is your best option. At first glance, it would be more obvious to other people what you're trying to do.
ISNULL(FLOOR(t1.UserPercentage), 0) AS UserPercentage
Examples:
SELECT FLOOR(1.1), FLOOR(1.9)
-- Result is a simple truncation of the decimal points. (=1)
declare #val decimal(9,2) =43.44
select convert(int, #val)
or
select cast(#val as int)
The following SQL,
declare #a as float, #b as float
select #a=1.353954 , #b=1.353956
select
CAST(#a as VARCHAR(40)) AS a_float_to_varchar ,
CAST(#b as VARCHAR(40)) AS b_float_to_varchar
results in
a_float_to_varchar b_float_to_varchar
---------------------------------------- ----------------------------------------
1.35395 1.35396
based on 'float' and 'real' (Transact-SQL).
Float has a precision of 15 digits, so I am not sure why the number is being rounded when converted to varchar.
Also from your link (it's actually the first line):
Approximate-number data types...
If you want exact precision, don't use float.
That being said, there is a function STR() specifically for converting float to a character data type.
Cast to decimal before casting to varchar:
declare #a as float, #b as float
select #a=1.353954 , #b=1.353956
select
CAST(CAST(#a AS DECIMAL(38,18)) as VARCHAR(40)) AS a_float_to_varchar ,
CAST(CAST(#b AS DECIMAL(38,18)) as VARCHAR(40)) AS b_float_to_varchar
You can specify style to include more digits.
declare #gg float
set #gg = 124.323125453
SELECT #gg,Convert(varchar, #gg,128)
For newer versions of SQL Server, use SELECT #gg,Convert(varchar, #gg,3)
returns
124.323125453 124.323125453
Reference: CAST and CONVERT (Transact-SQL)
Or with STR():
declare #gg float
set #gg = 124.323124354234524
SELECT #gg,str(#gg,16,15)
It should give you all the possible digits. 16 is the total possible length (includes period) while 15 places after the decimal is possible (actually 0.2323... the 0 count toward length, so the length needs to be 17 if all numbers are less that 1). STR(), however, pads the results with leading spaces and trailing 0.
I have a table that looks like this:
id datatype name value
0 nvarchar(255) myName 'Paul'
1 int age '30'
2 float(53) Custom1 0.5
3 float(53) Custom2 1.3
4 float(53) Custom3 2.7
I am wondering if it is possible to do something like the following where I cast the order by as a float - I know this is incorrect syntax but wondering if this can be done.
SELECT datatype, name, value FROM myTable
ORDER BY (float)name
Thanks in advance.
To convert datatypes in SQL you can use CAST or CONVERT:
SELECT CAST('123' AS FLOAT), CONVERT(FLOAT, '123')
But if your name field contains a value that cannot be converted to a valid float, you'll get an error.
e.g.
SELECT CAST('NotAValidFloat' AS FLOAT)
will give you:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.