SQL syntax - division and ceiling functions [duplicate] - sql

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 2 years ago.
I am trying to calculate how many packs of sweats a kid needs based on the amount of individual sweets required. I am trying to figure out the SQL syntax to do this.
I have used ceiling with divide but still not working
select CEILING(NoOfSweets / SweetsPerPack) AS NoOfPacks
Scenarios:
NoOfSweets SweetsPerPack RequiredOutCome NoOfPacks
--------------------------------------------------------
10 10 1 1
5 10 1 0
20 10 2 2
8 10 1 0
7 5 2 1

If the values are integers, then SQL Server does integer division. So, 1/2 is 0 rather than 0.5. I find that the simplest way to get a number with decimal points is to multiply by 1.0:
CEILING(NoOfSweets * 1.0 / SweetsPerPack)

Related

How to change values in a column from object type to float. for Example, "'€220 M" to 220,000,000? [duplicate]

This question already has answers here:
Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe
(6 answers)
Closed 3 years ago.
I have a column with data as:
1 77M
2 118.5M
3 72M
4 102M
5 93M
6 67M
I need to change this to its numerical value as:
77,000,000
and so on.
I have tried different ways but could not come up with a definite solution.
okay this should work
(df[1].str.replace('M','').astype(float) * 1000000).astype(int).astype(str).apply(lambda x : x[:-6]+','+x[-6:-3]+','+x[-3:])
Output
0 77,000,000
1 118,500,000
2 72,000,000
3 102,000,000
4 93,000,000
5 67,000,000
Name: 1, dtype: object

Oracle PL/SQL number to_char ignores 0's even when decimal part exist [duplicate]

This question already has an answer here:
Fetching value from a number column removes 0 before decimal
(1 answer)
Closed 5 months ago.
Im using the following code to convert a number(38,2) to the format "XXX.XXX.XXX,XX":
TO_CHAR(number, '999G999G999G999G999G999G999D99')
Although, when the number is 0 or 0.XX the 0 is eaten up :
Input: 0
Result: ",00"
Expected Result: "0,00"
Input: 0.23
Result: ",23"
Expected Result: "0,23"
Why does this happen, is there a way of solving this without manipulating the result string ?
Use 0 instead of 9
TO_CHAR(0.00, '999G999G999G999G999G999G990D99')
0 ensures that if there is no digit in that place it'll display 0.

I wanted to display numbers as alphanumberics [duplicate]

This question already has answers here:
how to display number value in words
(4 answers)
Closed 6 years ago.
Suppose in table I have a number column as
1
2
3
4
I want to display those rows as
one
two
three
four
How can I do using SQL
You can use a technique from this blog which uses a hack with dates to get the text version of numeric fields. The blog post goes into much more detail, but in short, it converts the number to a Julian date which lets TO_CHAR use the format specifier sp (spelling out in text)
SELECT num, TO_CHAR(TO_DATE(num, 'J'), 'Jsp') num_as_text
FROM myTable
ORDER BY num;
# num num_as_text
# ----------------
# 1 One
# 2 Two
# 3 Three
# 4 Four
You could use the j --> jsp technique to spell the number. It's been a FAQ.
j = julian. take the number and pretend it is a julian date, convert
it into a date.
jsp = Take that date and spell the julian number it represents.
For example,
SQL> SELECT LEVEL,
2 to_char(to_date(LEVEL,'j'), 'jsp') num_spell
3 FROM dual
4 CONNECT BY LEVEL <= 10;
LEVEL NUM_SPELL
---------- ----------
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
10 rows selected.
SQL>
Assuming the schema is structured as you suggest like:
Table
1 2 3 4
value value value value
value value value value
value value value value
You can use AS to rename the column in a query:
SELECT 1 AS one
2 AS two
3 AS three
4 AS four
FROM table

Configure float with decimal in sql or force string to show decimal [duplicate]

This question already has answers here:
Is there a way to cast float as a decimal without rounding and preserving its precision?
(3 answers)
Closed 9 years ago.
I have a table in SQl with a column in float type , this table is used to send values to a one fiscal printer,
code name price
----------------------------------
34 cUP 2,5
36 BOOK 2
37 COMET 1,2
38 TOY 1
IS posible configure SQl to show 1,00 o 2,00 when the value have not cents.
When i send to the printer i use this line :
string preco = vercup.Rows[i]["unitario"].ToString();
how can i force to show 1,00 when the values comes 1.
How you store the data isn't related to how the data is presented. yes, when you present the data you can force it to display two decimals.
select convert(decimal(9,2), price) from table
That's just 1 possible solution.

Understanding The Modulus Operator %

I understand the Modulus operator in terms of the following expression:
7 % 5
This would return 2 due to the fact that 5 goes into 7 once and then gives the 2 that is left over, however my confusion comes when you reverse this statement to read:
5 % 7
This gives me the value of 5 which confuses me slightly. Although the whole of 7 doesn't go into 5, part of it does so why is there either no remainder or a remainder of positive or negative 2?
If it is calculating the value of 5 based on the fact that 7 doesn't go into 5 at all why is the remainder then not 7 instead of 5?
I feel like there is something I'm missing here in my understanding of the modulus operator.
(This explanation is only for positive numbers since it depends on the language otherwise)
Definition
The Modulus is the remainder of the euclidean division of one number by another. % is called the modulo operation.
For instance, 9 divided by 4 equals 2 but it remains 1. Here, 9 / 4 = 2 and 9 % 4 = 1.
In your example: 5 divided by 7 gives 0 but it remains 5 (5 % 7 == 5).
Calculation
The modulo operation can be calculated using this equation:
a % b = a - floor(a / b) * b
floor(a / b) represents the number of times you can divide a by b
floor(a / b) * b is the amount that was successfully shared entirely
The total (a) minus what was shared equals the remainder of the division
Applied to the last example, this gives:
5 % 7 = 5 - floor(5 / 7) * 7 = 5
Modular Arithmetic
That said, your intuition was that it could be -2 and not 5. Actually, in modular arithmetic, -2 = 5 (mod 7) because it exists k in Z such that 7k - 2 = 5.
You may not have learned modular arithmetic, but you have probably used angles and know that -90° is the same as 270° because it is modulo 360. It's similar, it wraps! So take a circle, and say that its perimeter is 7. Then you read where is 5. And if you try with 10, it should be at 3 because 10 % 7 is 3.
Two Steps Solution.
Some of the answers here are complicated for me to understand. I will try to add one more answer in an attempt to simplify the way how to look at this.
Short Answer:
Example 1:
7 % 5 = 2
Each person should get one pizza slice.
Divide 7 slices on 5 people and every one of the 5 people will get one pizza slice and we will end up with 2 slices (remaining). 7 % 5 equals 2 is because 7 is larger than 5.
Example 2:
5 % 7 = 5
Each person should get one pizza slice
It gives 5 because 5 is less than 7. So by definition, you cannot divide whole 5items on 7 people. So the division doesn't take place at all and you end up with the same amount you started with which is 5.
Programmatic Answer:
The process is basically to ask two questions:
Example A: (7 % 5)
(Q.1) What number to multiply 5 in order to get 7?
Two Conditions: Multiplier starts from `0`. Output result should not exceed `7`.
Let's try:
Multiplier is zero 0 so, 0 x 5 = 0
Still, we are short so we add one (+1) to multiplier.
1 so, 1 x 5 = 5
We did not get 7 yet, so we add one (+1).
2 so, 2 x 5 = 10
Now we exceeded 7. So 2 is not the correct multiplier.
Let's go back one step (where we used 1) and hold in mind the result which is5. Number 5 is the key here.
(Q.2) How much do we need to add to the 5 (the number we just got from step 1) to get 7?
We deduct the two numbers: 7-5 = 2.
So the answer for: 7 % 5 is 2;
Example B: (5 % 7)
1- What number we use to multiply 7 in order to get 5?
Two Conditions: Multiplier starts from `0`. Output result and should not exceed `5`.
Let's try:
0 so, 0 x 7 = 0
We did not get 5 yet, let's try a higher number.
1 so, 1 x 7 = 7
Oh no, we exceeded 5, let's get back to the previous step where we used 0 and got the result 0.
2- How much we need to add to 0 (the number we just got from step 1) in order to reach the value of the number on the left 5?
It's clear that the number is 5. 5-0 = 5
5 % 7 = 5
Hope that helps.
As others have pointed out modulus is based on remainder system.
I think an easier way to think about modulus is what remains after a dividend (number to be divided) has been fully divided by a divisor. So if we think about 5%7, when you divide 5 by 7, 7 can go into 5 only 0 times and when you subtract 0 (7*0) from 5 (just like we learnt back in elementary school), then the remainder would be 5 ( the mod). See the illustration below.
0
______
7) 5
__-0____
5
With the same logic, -5 mod 7 will be -5 ( only 0 7s can go in -5 and -5-0*7 = -5). With the same token -5 mod -7 will also be -5.
A few more interesting cases:
5 mod (-3) = 2 i.e. 5 - (-3*-1)
(-5) mod (-3) = -2 i.e. -5 - (-3*1) = -5+3
It's just about the remainders. Let me show you how
10 % 5=0
9 % 5=4 (because the remainder of 9 when divided by 5 is 4)
8 % 5=3
7 % 5=2
6 % 5=1
5 % 5=0 (because it is fully divisible by 5)
Now we should remember one thing, mod means remainder so
4 % 5=4
but why 4?
because 5 X 0 = 0
so 0 is the nearest multiple which is less than 4
hence 4-0=4
modulus is remainders system.
So 7 % 5 = 2.
5 % 7 = 5
3 % 7 = 3
2 % 7 = 2
1 % 7 = 1
When used inside a function to determine the array index. Is it safe programming ? That is a different question. I guess.
Step 1 : 5/7 = 0.71
Step 2 : Take the left side of the decimal , so we take 0 from 0.71 and multiply by 7
0*7 = 0;
Step # : 5-0 = 5 ; Therefore , 5%7 =5
Modulus operator gives you the result in 'reduced residue system'. For example for mod 5 there are 5 integers counted: 0,1,2,3,4. In fact 19=12=5=-2=-9 (mod 7). The main difference that the answer is given by programming languages by 'reduced residue system'.
lets put it in this way:
actually Modulus operator does the same division but it does not care about the answer , it DOES CARE ABOUT reminder for example if you divide 7 to 5 ,
so , lets me take you through a simple example:
think 5 is a block, then for example we going to have 3 blocks in 15 (WITH Nothing Left) , but when that loginc comes to this kinda numbers {1,3,5,7,9,11,...} , here is where the Modulus comes out , so take that logic that i said before and apply it for 7 , so the answer gonna be that we have 1 block of 5 in 7 => with 2 reminds in our hand! that is the modulus!!!
but you were asking about 5 % 7 , right ?
so take the logic that i said , how many 7 blocks do we have in 5 ???? 0
so the modulus returns 0...
that's it ...
A novel way to find out the remainder is given below
Statement : Remainder is always constant
ex : 26 divided by 7 gives R : 5
This can be found out easily by finding the number that completely divides 26 which is closer to the
divisor and taking the difference of the both
13 is the next number after 7 that completely divides 26 because after 7 comes 8, 9, 10, 11, 12 where none of them divides 26 completely and give remainder 0.
So 13 is the closest number to 7 which divides to give remainder 0.
Now take the difference (13 ~ 7) = 5 which is the temainder.
Note: for this to work divisor should be reduced to its simplest form ex: if 14 is the divisor, 7 has to be chosen to find the closest number dividing the dividend.
As you say, the % sign is used to take the modulus (division remainder).
In w3schools' JavaScript Arithmetic page we can read in the Remainder section what I think to be a great explanation
In arithmetic, the division of two integers produces a quotient and a
remainder.
In mathematics, the result of a modulo operation is the
remainder of an arithmetic division.
So, in your specific case, when you try to divide 7 bananas into a group of 5 bananas, you're able to create 1 group of 5 (quotient) and you'll be left with 2 bananas (remainder).
If 5 bananas into a group of 7, you won't be able to and so you're left with again the 5 bananas (remainder).