This question already has answers here:
Sql Round, when on .5, rounds to the greater number ie 1.235 result 1.24 [duplicate]
(2 answers)
Closed 5 years ago.
I'm not sure that this is a duplicate question.
I need to ROUND a value only if the succeeding value is greater than 5.
For example:
If i have 123.4575, then the rounded value should be 123.457.
If i have 123.4576, then the rounded value should be 123.458.
But the default ROUND is not working as i'm expecting. See the below query,
select cast(round(123.4575, 3) as decimal(18,3))
Result:
123.458 (where it needs to be 123.457)
I need only three decimal points.
I prefer to suggest me some in-built functions rather than writing functions on my own.
Thanks.
This has been asked (many times). 5 is always rounded up by SQL Server. If you do not want to write your own routine look at Minh's answer here. This shows a neat trick to achieve what you want using a case statement and FLOOR.
Also an easy alternative is to subtract 1 from your number first at a precision 1 greater than your rounding, thus:
SELECT CAST(ROUND(123.4575 - 0.00001, 3) as decimal(18,3))
gives 123.457, whilst
SELECT CAST(ROUND(123.4576 - 0.00001, 3) as decimal(18,3))
gives 123.458
Related
This question already has answers here:
SQL Server, division returns zero
(6 answers)
Closed 1 year ago.
SELECT RecruitmentSource, COUNT(RecruitmentSource) AS NumberOfHired, COUNT(RecruitmentSource)/SUM(COUNT(RecruitmentSource)) OVER () AS PercentageHired
FROM HRDataset_v14$
GROUP BY RecruitmentSource
Percentage Hired that I expect as below link
FYI, Total Number of Hired is 311 so I expect the number would be like
1. 0.0739
2. 0.0932
3. 0.0997
.
.
.
9. 0.0418
Please help me to solve it.
SQL Server does integer division. I usually just multiply by 1.0 to avoid this:
SELECT RecruitmentSource, COUNT(RecruitmentSource) AS NumberOfHired,
COUNT(RecruitmentSource) * 1.0/SUM(COUNT(RecruitmentSource)) OVER () AS PercentageHired
FROM HRDataset_v14$
GROUP BY RecruitmentSource
This question already has answers here:
Dividing 2 numbers returns 0 [duplicate]
(3 answers)
How to calculate percentage with a SQL statement
(13 answers)
Division of integers returns 0
(2 answers)
Closed 3 years ago.
I am trying to perform a maths equation in SQL to calculate service up time. I have a column with pass counts and a column with fail counts.
I have achieved this sum in VB for the application side and the sum is as follows: pass / (pass+fail) * 100.
The results should be on some rows 99.98 but I can't get SQL to give me the same result. I either get 100 or 0 as the result.
This is my SQL query:
select pass / sum(pass+fail) * 100 as total, friendlyname from sensors group by pass, friendlyname
Example row:
Friendly Name | Pass | Fail
_____________________________
Cloudflare | 25527 | 23
So as you can see the result should be 99.90 when done on a calculator using above formula but SQL reports this sum as 0.
Really would be grateful for some help!
Make at least one portion of the division a floating point number, to force that precision. Also, your math seems to be off. If you want to compute the ratio of passes/fails to the total counts, then sums should appear in both the numerator and denominator.
select
100.0 * sum(pass) / sum(pass+fail) as pass_pct,
100.0 * sum(fail) / sum(pass+fail) as fail_pct
friendlyname
from sensors
group by
friendlyname;
Try this :
select
(pass * 100.0) / sum(pass+fail) as total,
friendlyname
from sensors
group by friendlyname, pass
This question already has answers here:
SQL Server round after division
(2 answers)
Closed 5 years ago.
Is there any way to return 3 as a result of ((5168/2000) .
I'm using CEILING(5168/2000) to delete rows. Its returning 2. But I want 3.
So that loop execute for 3 times.
Is there any way to return 3 as a result of (5168/2000)
Yes, make sure you're diving floating point numbers not integers
CEILING(5168.0/2000)
One way to achive that, aside from hardcoding the .0 as I have above, is to cast your integer to an appropriate type, for example:
CEILING(CAST(5168 AS FLOAT)/2000)
or muultiply it by a decimal
CEILING((1.0 * 5168)/2000)
This question already has answers here:
How Can I Sort A 'Version Number' Column Generically Using a SQL Server Query
(4 answers)
Closed 5 years ago.
I have a column of nvarchar(255) type that represents software version
numbers:
VersionNumber
---------------
1.0.0.505
1.0.0.506
1.0.0.507
1.0.0.508
1.0.0.509
1.0.1.2
I need to extract the maximum version number (the min version number in the example above is 1.0.0.505 and the max version number is 1.0.1.2, values arranged from the smallest to the highest).
in order to explain exactly what i need - if i could use imperative programming language i think i would do something like that to detect the max version number:
lets say version number is d.c.b.a.
i would separate each version number to four different variables: a b
c d
that i will sum each series.
a will be summed by tens
b will be summed by hundreds
c will be summed by thousands
d will be summed by milions
than the maximum total sum of each Max(a+b+c+d) will be the max version.
but what is the technic to achieve something like that in sql?
for future readers: based on #AlexK. link that is the solution:
select TOP 1 VersionNumber from Users order by (cast('/' + replace(VersionNumber , '.', '/') + '/' as hierarchyid)) DESC;
try this
select max(replace(version,'.','')) from yourtable
This question already has answers here:
Pad a string with leading zeros so it's 3 characters long in SQL Server 2008
(18 answers)
Closed 8 years ago.
I have two tables with product numbers. They both are limited to 12 characters (varchar(12)). One of them (product A) has a number structure like this:
Product No:
2345
568
89
And product B has the same exact numbers but with zeros to fill the 12 characters missing. It is something like this:
Product No:
000000002345
000000000568
000000000089
I just want to modify product A table to add the zeros at the beginning of the sequence. I had an idea with REPLACE() function but to add the zeros I might need another function. Thanks for reading and sorry for the time.
Try this, you can use this statement
RIGHT('000000000000'+ISNULL(ProductNo,''),12)
This should do it:
UPDATE tblA
SET ProductNo = REPLICATE('0', 12 - LEN(ProductNo)) + ProductNo
I just want to modify product A table to add the zeros at the beginning of the sequence.
Big hairy question for you: Are you absolutely certain that you want to store these values in the table with leading zeros, or just be able to display it as-needed with leading zeros?
Reason I ask is because the varchar(12) implies 13 bytes in memory, where an int only takes 4, which will make a big difference if this column participates in indexes and foreign key relationships with other tables.