SQL ceil

Created By: chatGPT

CEIL (or CEILING) is a mathematical function in SQL that is used to round a number upward to the nearest integer. Regardless of the decimal portion, CEIL always rounds up. It is useful in scenarios where you want to ensure that you do not fall below a whole number, often used in calculations involving quantities or items that cannot be represented as fractions. Most SQL database systems support this function. Here’s how to use it in different SQL implementations.
SELECT CEIL(5.3) AS RoundedUp;
-- Output: 6

SELECT CEIL(-4.7) AS Rounded;
-- Output: -4
Here's an example that might be more practical. Suppose we have a table named orders which contains information on items ordered and their respective prices. If you want to calculate the total price after applying a discount and you want each item price to be a whole number, you can apply the CEIL function.
SELECT item_id,
       price,
       CEIL(price * 0.9) AS DiscountedPrice
FROM orders;
In addition to basic usage, you can also incorporate CEIL within more complex expressions. For instance, if you are calculating a value that requires rounding twice, you can nest the CEIL function or combine it with other mathematical functions.
SELECT CEIL(SUM(quantity * price) * 1.05) AS TotalCost
FROM orders;
When using CEIL, keep in mind that the function returns a numeric value. If you are working with different numeric types, be sure to handle the resulting type accordingly. For instance, a floating-point number results in a whole number, needing type conversion if necessary.
SELECT CEIL(100.75) AS RoundedUp,
       CAST(CEIL(100.75) AS INT) AS RoundedInt;
Introduction And SetupSelectFromWhereJoinInner JoinLeft JoinRight JoinFull JoinOnGroup ByHavingOrder ByLimitInsert IntoValuesUpdateSetDeleteCreate TableAlter TableDrop TableCreate IndexDrop IndexUnionUnion AllIntersectExceptCaseWhenThenElseEndInBetweenLikeIs NullIs Not NullExistsNot ExistsAndOrNotAsWithDistinctCountSumAvgMinMaxCastCoalesceNullifSubstringConcatLowerUpperTrimReplaceRoundFloorCeilAbsModCurrent_dateCurrent_timeCurrent_timestamp