T-SQL: Convert money to float - sql-server

How can I convert (money/int)*100 to a float datatype?
a.ResidualValue is of type money, while ListPriceCar is of type int.
The following outputs a number with a comma as the decimal separator, so I presume it is still a money field, and not a float as I wish:
CONVERT(float, CONVERT(float, REPLACE(CONVERT(nvarchar(10), a.ResidualValue), ',', '.'))/ListPriceCar)*100 AS 'Residual value %'
Result: 62,825

What is issue with simple cast as below:
DECLARE #money AS money
SET #money = $2345234.35
SELECT #money, CAST(#money AS float)

What's the value of ResidualValue? If ResidualValue contains a comma, then comma is your decimal char. You shouldn't convert it into a point then. You can divide a money value by an int value.
declare #a money = 10.5;
declare #b int = 20;
select (#a / #b) * 100
select cast((#a / #b) * 100 as float)

In your example, the result was already given as an float. You can test this by running the following example code.
--INPUT VARIABLES
declare #ResidualValue money = 5000.54
declare #ListPriceCar int = 15000
--OUTPUT VARIABLE
declare #ResultCalculation as sql_variant;
--CALCULATION
set #ResultCalculation = CONVERT(float, CONVERT(float, REPLACE(CONVERT(nvarchar(10), #ResidualValue), ',', '.'))/#ListPriceCar)*100
--GET INFORMATION ON THE OUTPUT VARIABLE
select sql_variant_property(#ResultCalculation,'BaseType') AS 'Base Type',
sql_variant_property(#ResultCalculation,'Precision') AS 'Precision',
sql_variant_property(#ResultCalculation,'Scale') AS 'Scale',
sql_variant_property(#ResultCalculation,'TotalBytes') AS 'TotalBytes',
sql_variant_property(#ResultCalculation,'Collation') AS 'Collation',
sql_variant_property(#ResultCalculation,'MaxLength') AS 'MaxLength';
There is an easier way to calculate your answer:
CAST(#ResidualValue/#ListPriceCar * 100 as float)

Related

Cast everything for 2 decimal points?

I want to divide two integers: 8/15 and return "53.33".
I've tried every single combination, and I've found that this is the only way I can return the desired value:
select cast(100*cast(8/cast(15 as decimal(10,4)) as decimal(18,4)) as decimal(18,2))
Is there a shorter way?
Thanks.
You are doing interger division. Use float, and multiply by 100.
select 8/15.00 * 100
Or, for your precision, just do the cast once
select cast(8/15.00 * 100 as decimal (10,2))
If these are integers in a table then multiple by 1.0 and cast once
DEMO
declare #table table (int_one int, int_two int)
insert into #table
values
(8,15)
select
cast(((int_one * 1.0) / int_two) * 100 as decimal(10,2))
from #table
try this:
select (cast (8 as money)/cast(15 as money) * 100)

How to get the division of 2 integers not to return 0

why SET #H1 = CAST((#rCount / #poolTot) AS VARCHAR(50)) + '%' keeps returning 0%?
#H1 is a VARCHAR(50) while #rCount and #poolTot are integers with values of 8 and 10. I should get something like .80%
Thanks for helping
Either your dividend or divisor (or both) need to be a datatype with decimal places (decimal, float, etc). One way of doing this is to muliply one of the numbers by 1.0, to force the conversion. Or you can do an explicit conversion.
Your results may vary, depending on the data type you choose. Using the first method below, you will get "0.80000000000000%". If you cast it cast #poolTot as a float, like the second example, you will get "0.8%". It really depends on what you want your results to look like.
DECLARE #H1 VARCHAR(50)
,#rCount INT = 8
,#poolTot INT = 10
SET #H1 = CAST(((#rCount)/ (#poolTot * 1.0)) AS VARCHAR(50)) + '%'
SELECT #H1
_
DECLARE #H1 VARCHAR(50)
,#rCount INT = 8
,#poolTot INT = 10
SET #H1 = CAST(((#rCount)/ cast(#poolTot as float)) AS VARCHAR(50)) + '%'
SELECT #H1
If you want to convert the result in decimal you can do something like
Declare #rCount as int=8;
declare #poolTot as int =10
select CAST((CONVERT(DECIMAL(10,2), #rCount)) /
(CONVERT(DECIMAL(10,2), #poolTot))as varchar(50)) + '%'
Output will be like
Again if you want only two digit after decimal you can do something like
Declare #rCount as int=8;
declare #poolTot as int =10
declare #Temp as decimal(10,2)=(CONVERT(DECIMAL(10,2), #rCount)) /
(CONVERT(DECIMAL(10,2), #poolTot))
select convert(varchar(10),#Temp,2) + '%'
Than it will display like
I think you can use this:
SET #H1 = CAST((100 * #rCount / #poolTot) AS VARCHAR(50)) + '%'

To convert float value to string

I am using two columns which have Date and float values, now I need to combine both the columns as a single string. For date I am able to convert it to string. But for the float values it is rounding up the decimal value which should not be the case.
For example my float value is 204.8 and date id 2014-11-11. Now when I combine as string it should result as '2014-11-11 204.8' But it is showing me as '2014-11-11 204' when I convert float value. I am using this query,
DECLARE #myDateTime DATETIME
DECLARE #StandardCost INT
SET #myDateTime = '2011-12-24 00:00:00.000'
SET #StandardCost = 204.8
SELECT LEFT(CONVERT(VARCHAR, #myDateTime, 120), 10)+ ' ' + CAST(#StandardCost AS VARCHAR(MAX))
Result: 2011-12-24 204
Can anyone help me in getting exact float value with decimals as string.
You have #StandardCost defined as an int. It will not hold the decimal portion. Change that to a numeric or some other datatype that will support decimal places.
First, you need to declare its as FLOAT, instead of INT
Then, you can try using STR() function
DECLARE #StandardCost FLOAT
SET #StandardCost = 204.8
SELECT STR(#StandardCost,10,1)
As #Sean Lange said change the INT variable to a FLOAT
DECLARE #myDateTime DATETIME
DECLARE #standardcost FLOAT; -- <-------- Changed INT to FLOAT
SET #mydatetime = '2011-12-24 00:00:00.000';
SET #standardcost = 204.8;
SELECT LEFT(CONVERT(VARCHAR, #mydatetime, 111), 10) + ' ' + CAST(#standardcost AS VARCHAR(MAX));

How to add or concatenate money type and string on query mssql

I have a situation like this
I got a column with 'money' type, 2 decimal . Example data:(65.00)
I need to add 12 zero / 000000000000 to it so that the output would be like this:
(65.00 convert to 6500) + (000000000000) = 000000006500
Output: 000000006500
How can I achieve this?. Thank you for your help and suggestion
You can do this with a couple of casts, multiplying by 100, and using REPLICATE('0') to pad with the requisite number of zeroes).
I'm assuming you DO want up to 2 x trailing decimals, but no more.
DECLARE #value MONEY;
SET #value = 65.123;
DECLARE #intValue BIGINT;
SET #intValue = CAST(#value * 100.0 AS BIGINT);
SELECT REPLICATE('0',12-LEN(#intValue)) + CAST(#intValue AS NVARCHAR(20));
Returns 000000006512
If you need to do this on a set, a CTE can be used for the intermediate step, e.g.
WITH cte AS
(
SELECT CAST(MoneyField * 100.0 AS BIGINT) AS intValue
FROM SomeTable
)
SELECT
REPLICATE('0',12-LEN(cte.intValue)) + CAST(cte.intValue AS NVARCHAR(20))
FROM cte;
Fiddle here
It is Possible .But output Column should be in the type of varchar(15) .If you want to do further operation of your output you have to convert that into int or whatever
SELECT CONCAT(REPEAT('0',12-LENGTH(65.00)),(65.00*100));

mssql convert varchar to float

I have a field value productlength of 0.123. This is from a view and has a data type of varchar.
I need to convert it to a float or numeric value so as o perform math comparisons.
convert(float,productlength)
and
cast(productlength as float) both do not work.
error varchar cant be converted to float or somethiing liek that.
From what I have read varchar can simply not be converted to a numeric string?
Any clever ways around this?
You can convert varchars to floats, and you can do it in the manner you have expressed. Your varchar must not be a numeric value. There must be something else in it. You can use IsNumeric to test it. See this:
declare #thing varchar(100)
select #thing = '122.332'
--This returns 1 since it is numeric.
select isnumeric(#thing)
--This converts just fine.
select convert(float,#thing)
select #thing = '122.332.'
--This returns 0 since it is not numeric.
select isnumeric(#thing)
--This convert throws.
select convert(float,#thing)
Use
Try_convert(float,[Value])
See
https://raresql.com/2013/04/26/sql-server-how-to-convert-varchar-to-float/
DECLARE #INPUT VARCHAR(5) = '0.12',#INPUT_1 VARCHAR(5)='0.12x';
select CONVERT(float, #INPUT) YOUR_QUERY ,
case when isnumeric(#INPUT_1)=1 THEN CONVERT(float, #INPUT_1) ELSE 0 END AS YOUR_QUERY_ANSWERED
above will return values
however below query wont work
DECLARE #INPUT VARCHAR(5) = '0.12',#INPUT_1 VARCHAR(5)='0.12x';
select CONVERT(float, #INPUT) YOUR_QUERY ,
case when isnumeric(#INPUT_1)=1 THEN CONVERT(float, #INPUT_1) ELSE **#INPUT_1** END AS YOUR_QUERY_ANSWERED
as #INPUT_1 actually has varchar in it.
So your output column must have a varchar in it.

Resources