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)
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:
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
This question already has an answer here:
BigQuery check for array overlap
(1 answer)
Closed 5 years ago.
I have an array of values I'd like to filter results for if an array column contains any of these values, is there an easy way to perform an intersect in BigQuery using the standard SQL language?
This should give you the general direction:
SELECT ...
WHERE EXISTS(SELECT 1 FROM UNNEST(array_column) a WHERE a IN UNNEST(array_values))
This question already has answers here:
How to get min/max of two integers in Postgres/SQL?
(2 answers)
Closed 6 years ago.
is there some way to determine the max value out of two values in SQL?
I use the mod function:
MOD(cnt, cnt/100)
This yields a division by 0 error when cnt is smaller than 100. I therefore would like something like this:
MOD(cnt, MAX(cnt/100, 1))
You can use greatest
SELECT greatest(a, b, c) FROM your_table;
This question already has answers here:
Counting chars in sequences via SQL
(2 answers)
Closed 7 years ago.
In Postgres/SQL, how can I get the count of a character in a column?
For example, I want to run a query which will return the number of times "X" appears in a column that has the value "XXX" - and it should return 3.
One method is the difference of lengths:
select (length(col) - length(replace(col, 'X', ''))) as NumX