How do I convert a float to varchar without loss of decimal places? SQL Server 2012 - sql-server-2012

SQL Server 2012: converting a float to varchar is removing decimal places.
For example:
select convert(varchar, cast(834.7833 as float))
returns a varchar with 834.783.
How do I convert a float to varchar without loss of decimal places?
This is a similar question to this unanswered question:
How does convert(varchar, float) decide how many decimal places to keep?

Why do you need to do a CAST(... AS FLOAT) at all??
Try these snippets - they return the full number of decimal points:
SELECT
CONVERT(VARCHAR(20), 834.7833)
SELECT
CAST(834.7833 AS VARCHAR(20))
Both of these output this result:
834.7833

Related

Why do I have to cast a float to a decimal and then a decimal into a varchar?

Please see the DDL below:
create table #TestFloat (id int, floattest float)
insert into #TestFloat values (1,65478)
insert into #TestFloat values (1,65478888)
insert into #TestFloat values (1,42)
How would I cast the float values to a varchar? I have tried this:
select top 1 cast(floattest as varchar(15)) from #TestFloat
which produces this:
6.54789e+007
and this:
select top 1 id,Str(floattest, 15, 0) from #TestFloat
' 65478'
I have put quotes in the above to demonstrate that it is casted into a char.
This works (taken from this question: How to convert Float to Varchar in SQL):
select CAST(CAST(floattest AS DECIMAL(20)) AS VARCHAR(20)) from #TestFloat
Why do I have to cast into a decimal first?
How about
SELECT FORMAT(floattest,'R') FROM #TestFloat
(Or 'G' or 'N' depending on your use)
https://msdn.microsoft.com/library/dwhawy9k.aspx
or
SELECT LTRIM(STR(floattest,8,0)) FROM #TestFloat
(Pick your precision and scale to suit but watch out for rounding)
https://msdn.microsoft.com/en-AU/library/ms189527.aspx
You also ask "why"? You should probably have a read of the MSDN pages on SQL Server types starting here: https://msdn.microsoft.com/en-AU/library/ms187752.aspx and this answer Difference between numeric, float and decimal in SQL Server
You can use convert function in sql server
select top 1 CONVERT (NVARCHAR(15),floattest) from #TestFloat
First cast to decimal and then to varchar.
Query
SELECT CAST
(
CAST(floattest AS DECIMAL(25,0))
AS VARCHAR(MAX)
)
FROM #TestFloat;
SQL Fiddle demo

Rounding before converting to varchar

For presentations sake I have a value that I want to show to only two decimal places before converting to varchar
select '£' + cast(round(amount,2) as varchar (10))
This works fine and would display the result as, say, £300.00. However the 'amount' also needs to be divided by 100 as part of the query. When I add that in to the code...
select '£' + cast(round(amount/100,2) as varchar (10))
..it displays as £3.000000. Is there any way to remove the extra 0s so only two are shown after the decimal point?
Just use format():
select '£' + format(amount, 2)
This also adds in commas, which seems desirable.
If you don't want the commas, then don't use round(), cast to a decimal type:
select '£' + cast(cast(round(amount, 2) as decimal(10, 2)) as varchar(10))
round() does change the value, but it doesn't change the storage mechanism. The cast( . .. as varchar) doesn't know -- or care -- how many significant values are in the result.
EDIT (for SQL Server):
Instead of format() you can use the str() function:
select '£' + ltrim(str(amount, 10, 2))
Or the last method of converting to a decimal before the conversion.
What is the datatype of amount column? cast the round part into float
select '£' + cast(round(cast(8375.8734/100 as float),2) as varchar (100));
Result is
£83.76
Also this should done in the front end application and not in sql

How to format % and in 2 decimal points?

How do I code format the return data in 2 decimals and with percentage format like 100.00% or 67.39% instead of 100.000000 or 67.391304?
SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46 as 'C%'
I tried ROUND() but I got the error stating that the round function requires 2 to 3 arguments?
ROUND(SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46) as 'C%'
Thanks!
You can convert to a decimal your original value:
CONVERT(VARCHAR(20), CONVERT(DECIMAL(18,2), SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46) ) + '%' as 'C%'
The first number in the decimal represents the number of digits in the number including decimal places, and the second number represents the number of decimal places.
You should pass number of decimals in second parameter to round function. For formating you can cast number to money and then cast to varchar:
select cast(cast(ROUND(SUM(123.12321)*100.0/46, 2) as money) as varchar) + '%'
Using Round and Cast will work. First round to 2 decimal places then convert to a decimal with 2 places to truncate the excess zeros.
select cast(Round(yourValue, 2) as decimal(18,2))
Sql Fiddle
You can use Format function
select FORMAT(100.0000, 'N' , 'en-us')
returns 100.00
and
select FORMAT(67.391304, 'N' , 'en-us')
returns 67.39
EDIT
In version below 2012 you can do this
SELECT CAST(67.391304 AS NUMERIC(10, 2))
returns 67.39
You can just do:
select FORMAT(0.391304, '##0.00%')
But keep in mind that it implicitly multiplies by 100, so the above will display as 39.13%.

SQL Server 2005 money format

I have been using SQL Server 2005, and I need to store some columns in database in Money format.
But SQL Server stores them like this --> 0000.0000, but I only want 2 digits after the decimal point ---> 0000.00
Do I have to control this in the program or there is a way to change this format in SQL Server 2005 ?
Thanks in advance
I would normally do these formatting stuffs in the front end. But if you still have to do it within the DB layer itself then make use of ROUND function.
SELECT CAST(ROUND(1234.1234, 2) AS MONEY)
Would also suggest you to check out the Performance storage comparison between Money & Decimal post written by Aaron Bertrand: https://sqlblog.org/2008/04/27/performance-storage-comparisons-money-vs-decimal
Money Vs Decimal Vs Float Decision Flowchart (Extract from SQLCAT.com):
Source: http://sqlcat.com/sqlcat/b/technicalnotes/archive/2008/09/25/the-many-benefits-of-money-data-type.aspx
Use Round() function. Returns a numeric value, rounded to the specified length or precision.
More here.
Round(value,2,1)
Second parameter specifies precision to be two decimal places.
Third parameter with value one specifies truncate rather than round off.
try this it works for SQL Server 2008 and below (2012 have already a FORMAT() function that you can use)
this will only works for data type Money and SmallMoney
declare #v money -- or smallmoney
set #v = 1000.0123
select convert(varchar(25), #v, 0)
select convert(varchar(25), #v, 1)
select convert(varchar(25), #v, 2)
select convert(varchar(25), #v, 126)
select '$' + convert(varchar(25), #v, 0)
select '$' + convert(varchar(25), #v, 1)
select '$' + convert(varchar(25), #v, 2)
select '$' + convert(varchar(25), #v, 126)
Hope this help!

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