How to round with no trailing zeros in SQL Server 2005? - sql-server-2005

How to round with no trailing zeros in SQL Server 2005?
select round(100.5555, 2)
...yields 100.5500. How to get rid of the zeros?

Try this
select CAST(round(100.5555, 2) AS DECIMAL(8,2))

You could re-cast it as your original datatype, e.g.
SELECT CAST(ROUND(100.5555, 2) AS FLOAT)
However, this sounds like display logic and therefore, I suspect you are better off doing this within your UI rather than your DB.

declare #d decimal(8,2) can help you.

Related

Whole number to comma separated price - SQL Server & SSMS

I couldn't find any answer to my question and I hope you can help me out. I've been asked to create a revenue report, however, I'm dealing with an old poorly optimized database. It sounds like a simple thing but I cannot find any solution to the below issue.
Basically, some prices are being shown in the following format:
22400
What I need is to show these values in this format format: 22,40
Any suggestions? Thanks in advance.
You can use format function :
select format(prices, '0,000')
from table t;
Try
convert(decimal(38, 2), convert(decimal, 22400) / 1000);
to convert it to a decimal(38, 2). Possibly lower the precision (38) if you don't need that much.
First you have to convert this price(ex: 22400) from one datatype to 'money' datatype using CAST function.
and then CONVERT money to character
references -
1) https://www.techonthenet.com/sql_server/functions/convert.php
2) https://www.techonthenet.com/sql_server/functions/cast.php
Syntax :
CONVERT(nvarchar, CAST(price AS money), 1)

Convert nvarchar scientific notation value into number in SQL Server 2014

I have a column loaded into SQL Server with these nvarchar values:
ColumnName
==========
6.19e+014
.....
6.19e+014
Now, what would be the easiest way to convert this value into numbers again.
Kindly suggest. Thanks!
declare #String varchar(25)='6.19e+014'
Select cast(#String as float)
Returns 619000000000000
This will cast to FLOAT without issue:
SELECT CAST('6.19e+014' AS FLOAT)
If what you really want is the specific value that got converted to scientific notation in the first place, you'll have to go back in the process to before it got converted and fix that.
Also, try this
Select convert(numeric(15,0),ltrim(rtrim(str(column_name,15))))
from table_name

How to round off to 4 decimal places in SQL in such a way that zero gets appended if we have less that 4 digits after the coma

4.145 should be 4.1450, and 4.124489 be 4.1245, I'm using SQL Server 2012
A trailing zero can only exist in a string representation of a numerical value. In SQL Server you can use FORMAT():
SELECT FORMAT(4.145, '0.0000')
See SQLFiddle
Do it like this:
declare #num as float;
set #num=5.20;
select convert(decimal(10, 2), #num);
Or instead search this:
Float datatype with 2 digits after decimal point
Try to understand this one

SQL Server 2008 thousands separator for a column

I have a column called TotalArea and its format is numeric (12,2).
I want it to display the numbers with a thousand separator so when I
select TotalArea from table
to show me a format like 1,234.00.
How could I do that? Thanks!
SELECT FORMAT(12345,'#,0.00');
SELECT FORMAT(TotalArea,'#,0.00') from table;
Reference:
https://msdn.microsoft.com/en-us/library/ee634206(v=sql.105).aspx
Try this way:
SELECT REPLACE(CONVERT(VARCHAR, CONVERT(MONEY, TotalArea), 1), '.00', '')
FROM table
or
SELECT CAST(CONVERT(VARCHAR, CAST(123456 AS MONEY), 1) AS VARCHAR)
FROM table
Formatting numbers for display is something that should be done in the display layer, and not within the database. So, in whatever application this data ends up being used, you should format it there. Management Studio, unfortunately, does not offer much control in this regard.
I know the question is for sql server 2008 but if you have sql server 2012+, you can use format like so:
SELECT FORMAT(12345.5634, 2);
Reference: https://www.w3schools.com/sql/func_mysql_format.asp

SQL Round function not working, any ideas?

Here is the SELECT statement:
SELECT ROUND(ISNULL(SUM(Price),0),2) As TotalPrice
FROM Inventory
WHERE (DateAdded BETWEEN #StartDate AND #EndDate)
Any ideas of why it's not rounding to two decimal places?
instead of ROUND(ISNULL(SUM(Price),0),2) you could try CAST(ISNULL(SUM(PRICE),0) AS DECIMAL (4,2))
What datatype is Price?
ROUND in BOL
SELECT ROUND(123.4545, 2); -- = 123.4500
GO
SELECT ROUND(123.45, -2); -- = 100,00
GO
The underlying datatype stays the same: you merely round but leave trailing zeros.
For 2 decimal place output, you'd need to CAST to decimal(x, 2)
You might be having marshalling issues for the column in your environment. Might try an explicit cast CAST(ROUND(...) AS NUMERIC(18,4)) or even just try making 0 0.0. Make sure also you are binding the column with the proper datatype in your application.
All the cool people use COALESCE instead of ISNULL. COALESCE is portable and you can have as many parameters as you want (not just two!!)
Anyway I'm not sure if this was just an example but you may also have DA issues with your data if it had not already been rounded at this stage.