How to round off the below values in SQL Server? [duplicate] - sql

This question already has an answer here:
How to lower bound the value 6.15 to 6.1 in SQL Server?
(1 answer)
Closed 4 years ago.
How to round off the below values in SQL?
35.38
53.08
6.15
5.38
to
35.4
53.1
6.1
5.4

SELECT ROUND(colname - 0.01, 1) AS RoundValue FROM TableName;
SYNTAX OF ROUND():
ROUND ( numeric_expression , length [ ,function ] )
Demo
http://sqlfiddle.com/#!18/2f9d9/11

Have you tried round()?
select round(val, 1)
However, I would recomend converting to a decimal format:
select cast(val as decimal(10, 1))
The cast() not only changes the value, but it also changes the type. That means that tools should also print out only one decimal place.

Related

Rounding DOWN postgresql

Here is a example query, I've been trying to find a workaround for this but my knowledge for postgresql at this time is still limited. Thanks in advance.
Sample Query:
select round(3.041,2) as column
Expected Output:
3.03 instead of 3.04
Rounding UP works but I need it to round DOWN as well if the decimal value is <5.
You can round down with some simple decimal counting like
SELECT
3.04 - POWER(10,
(-1 *
CAST(
LENGTH(
SPLIT_PART(
CAST(3.04 AS STRING), '.', 2
)
)
AS INT)
)
)
However, I have no idea why you would do this. Are you sure you aren't trying to use the basic ROUND function and cut off more significant figures?

SQL Convert & Cast Nvarchar Time to a decimal

I'm working on a legacy database and need to parse info from one database to another, parsing it into the new database is easy enough but first I need to create the query to convert and cast the following in the legacy SQL Server database:
WorkedHours(NVARCHAR(10)) is in text format 07:30
I need to convert and cast this as a decimal ie 7.5
I have searched around for the answer to this but can not find anything that has worked, so thought I would put it out there to see if any of you has any ideas.
Edit - What I should of asked is. What is causing an error converting to an int from a character with a value of 0 when trying to trying to convert and cast a time to a decimal?
DATEDIFF(
MINUTE,
0,
CAST('07:30' AS TIME)
)
/
60.0
Works up to '23:59' only
EDIT:
Based on a comment elsewhere, you have some 'bad' values.
This may find them...
SELECT
*
FROM
yourTable
WHERE
TRY_CONVERT(TIME, worked_hours) IS NULL
And as such, this is a safer version of my expression....
DATEDIFF(
MINUTE,
0,
TRY_CONVERT(TIME, worked_hours)
)
/
60.0
(Returns NULL for values that failed to parse.)
There's no reason to pull out the date/time types. Just do some simple string parsing:
cast(left(right('0' + WorkedHours, 5), 2) as int)
+ cast(right(WorkedHours, 2) as int) / 60.00
This won't have any limitations on 24 hours or anything like that. It just assumes that you've got one or two digits before a colon and two digits after.
This should work in SQL Server and an example-string "1101:56" (1101h & 56 minutes) | in general from 0h to >24h:
-- Take all hours before ":" and all Minutes (2 digits) after ":" and convert it to decimal.
select convert(decimal,left('1101:56',CHARINDEX(':','1101:56')-1)) + ( convert(decimal,right('1101:56',2))/60 );
-- with column-placeholder "time_str_from_table"
select convert(decimal,left(time_str_from_table,CHARINDEX(':',time_str_from_table)-1)) + ( convert(decimal,right(time_str_from_table,2))/60 );
If the source table have NULL-Values, than use "ISNULL" with substitution-value "0.0":
-- with column-placeholder "time_str_from_table"
select isnull( ( convert(decimal,left(time_str_from_table,CHARINDEX(':',time_str_from_table)-1)) + ( convert(decimal,right(time_str_from_table,2))/60) ), 0.0);

Float to varchar with style 3 is failing in sql server 2016

I am getting Arithmetic overflow error while executing following query on sql server 2016.
"select convert(varchar(20), cast('0' as float), 3)"
The same query works fine on sql server 2014.
you are getting this error cause casting '0' to float of style 3 returning 23 charter value which is unable to convert to varchar of length 20 only.
try something like,
select convert(varchar(23), cast('0' as float), 3)
Or
select convert(varchar(max), cast('0' as float), 3)
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
The same query works fine
on sql server 2014.
style in sql 2014
Other values are processed as 0.
A maximum of 6 digits. Use in scientific notation, when appropriate.
style in sql 2016
3 Always 17 digits. Use for lossless conversion. With this style, every distinct float or real value is guaranteed to convert to a
distinct character string.Applies to: Azure SQL Database, and starting in SQL Server 2016.
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

MSSQL - Getting decimals in a division [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Decimal values in SQL for dividing results
(6 answers)
Closed 6 years ago.
I'm using MSSQL 2014 and I'd like to know if there is a better way to display 2 decimals in a simple division like 10/3.
SELECT 10/3 -- returns 3
SELECT CONVERT(DECIMAL(10, 2), 10/3) -- RETURNS 3
SELECT CAST(10/3 AS DECIMAL(10,2)) -- RETURNS 3
The only way I found to make it work is by casting the divisor as float:
SELECT 10/CAST(3 AS FLOAT) -- returns 3.333333...
SELECT CONVERT(DECIMAL(10, 2), 10/CAST(3 AS FLOAT)) -- RETURNS 3.33
SELECT CAST(10/CAST(3 AS FLOAT) AS DECIMAL(10,2)) -- RETURNS 3.33
Are these two last options the best approach available?. Is it possible to do this without any cast/conversion?
Yeah, to truncate you have to specify that you only want two digits. If it were me I'd do:
SELECT cast(10/3.0 as decimal(10,2))
I get a float if I do,
select 10/3.0
https://stackoverflow.com/a/11719098/28045
Division of one integer by another will result in an integer. Either cast the numbers or use variables of float or decimal type having values of 10 and 3.

Issue with DateDiff / Nb in SQL Server [duplicate]

This question already has answers here:
Integer division in sql server
(8 answers)
Closed 8 years ago.
When I use a DateDiff in a SELECT statement in SQL Server, I get an int value back:
select DATEDIFF(day,'01/01/2011','15/05/2011')
from Table1
Result is : 134
select DATEDIFF(day,'01/01/2011','15/05/2011')/15
from Table1
Result is : 8
select cast(DATEDIFF(day,'01/01/2011','15/05/2011') / 15 as Decimal(9,2))
from Table1
Result is : 8.00
But in reality the result is : 8.9333333
How can I change the result from int to Double?
I want to get the result 8.9333333 - how can I do this?
The problem is that you're using CAST after dividing. If you perform the division after the CAST, you'll get the answer you want:
select
cast(DATEDIFF(day,'01/01/2011','15/05/2011') as Decimal(9,2))/15
from Table1
You need to cast your inputs into the division as decimal first:
select cast(DATEDIFF(day,'2011-01-01','2011-05-15') as Decimal(9,2))/15.0
Otherwise you are dividing an integer by an integer - you'll get an integer out. If you then cast this to a decimal after the division, you are going to get 8.0.
SELECT CAST(DATEDIFF(day,'01/01/2011','15/05/2011') AS Decimal(9,2))/15
FROM Table1