🧚 Ακούστε! Το Beekeeper Studio είναι ένα γρήγορο, μοντέρνο και ανοιχτού κώδικα GUI βάσης δεδομένων Λήψη
February 14, 2024 Από Matthew Rathbone

Subtraction isn’t just for numerical operations! SQL Server has several built in functions and operators to help with subtraction for both numbers and dates. Plus unlike when working with pen and paper, we have to always be thinking about NULL. Let’s jump in with some examples.

Understanding Subtraction Operations in SQL Server

Subtraction operations in SQL Server typically come into play in two situations - performing arithmetic on numerical data and manipulating datetime values. You’ll often find subtracting values comes in handy when you need to generate calculated values directly within SQL Server.

Basic Subtraction

Let’s start with a simple subtraction operation to simplify the concept. Suppose we have a table called “Orders” containing the “TotalAmount” and “DiscountAmount”.

SELECT TotalAmount, DiscountAmount, 
TotalAmount - DiscountAmount AS ActualAmount 
FROM Orders;

In this basic example, we subtract “DiscountAmount” from “TotalAmount” and output the result as “ActualAmount”.

Using Subtraction Operations on TimeInterval

Subtraction is also frequently used to compute the difference between two datetimes. SQL Server has several date time types: smalldatetime, datetime, datetime2, datetimeoffset. We can use the DATEDIFF() function for date subtraction.

Example:

SELECT OrderID, OrderDate, 
DATEDIFF(day, OrderDate, GETDATE()) AS DaysElapsed 
FROM Orders;

This command will calculate and display the number of days elapsed since each order was placed to today (GETDATE returns today’s date).

Aggregating Result Sets with Subtraction in SQL Server

Aggregation of result sets using subtraction is also quite common in SQL Server. For example, you might need to aggregate the total sales for a period and subtract the total discounts given during the same period.

Example:

SELECT 
( SELECT SUM(TotalAmount) FROM Orders where OrderDate like '2022-%' ) - 
( SELECT SUM(DiscountAmount) FROM Orders where OrderDate like '2022-%' ) as NetSales

The command above will display the net sales for the year 2022 by subtracting total discounts from total sales.

Handling NULL

In SQL Server, subtracting from NULL returns a NULL value. It’s important to handle these cases to prevent unexpected NULLs in your results if you’d prefer to treat NULL the same as 0. This is not always true, so act wisely.

SELECT OrderID, 
ISNULL(TotalAmount, 0) - ISNULL(DiscountAmount, 0) AS NetAmount 
FROM Orders;

Now You’re A Subtraction Pro

There are other ways to effectively subtract value sin SQL Server, so keep playing around with these commands and consider exploring more on your own. Each query you construct will bring you a step closer to mastering SQL Server. Happy querying!

This article has been written with SQL Server in mind. Thus, all commands and explanations refer to SQL Server. For information on how these commands may work on other database engines, please consult those particular databases’ official documentation.

Το Beekeeper Studio Είναι Ένα Δωρεάν & Ανοιχτού Κώδικα GUI Βάσης Δεδομένων

Το καλύτερο εργαλείο SQL query & editor που έχω χρησιμοποιήσει. Παρέχει όλα όσα χρειάζομαι για να διαχειριστώ τη βάση δεδομένων μου. - ⭐⭐⭐⭐⭐ Mit

Το Beekeeper Studio είναι γρήγορο, διαισθητικό και εύκολο στη χρήση. Το Beekeeper υποστηρίζει πολλές βάσεις δεδομένων και λειτουργεί εξαιρετικά σε Windows, Mac και Linux.

Η έκδοση Linux του Beekeeper είναι 100% πλήρης, χωρίς περικοπές και χωρίς συμβιβασμούς στα χαρακτηριστικά.

Τι Λένε Οι Χρήστες Για Το Beekeeper Studio

★★★★★
"Το Beekeeper Studio αντικατέστησε εντελώς την παλιά μου ροή εργασίας SQL. Είναι γρήγορο, διαισθητικό και κάνει τη δουλειά με βάσεις δεδομένων απολαυστική ξανά."
— Alex K., Προγραμματιστής Βάσεων Δεδομένων
★★★★★
"Έχω δοκιμάσει πολλά GUIs βάσεων δεδομένων, αλλά το Beekeeper βρίσκει την τέλεια ισορροπία μεταξύ χαρακτηριστικών και απλότητας. Απλά δουλεύει."
— Sarah M., Full Stack Μηχανικός

Έτοιμοι να Βελτιώσετε τη Ροή Εργασίας σας με SQL;

download Δωρεάν Λήψη