Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
If I want to test a calculation to be used in an SQL SELECT statement I can use variables to allow me to use several test values without rewriting the calculation.
DECLARE @GrossPrice DECIMAL(18,2)
DECLARE @VATRate DECIMAL(18,2)
SET @GrossPrice = 100
SET @VATRate = 0.2
SELECT
@GrossPrice AS [Sale Price]
,@GrossPrice/(1+@VATRate) AS [Net Price]
,@GrossPrice - @GrossPrice/(1+@VATRate) AS [VAT]
Here is the output with three columns appropriately aliased.
The results of the calculations
In practice I would have CAST the results to DECIMAL(18,2) as well.
Once you are happy with the calculations a little bit of editing will allow it to be used on a real table.
SELECT
GrossPrice AS [Sale Price]
,GrossPrice/(1+VATRate) AS [Net Price]
,GrossPrice - GrossPrice/(1+VATRate) AS [VAT]
FROM tblSale
I am assuming tblSale had two columns with names almost identical to the variables and three rows of data.
The results of the calculation on a table
I hope the above methods help you write and test calculations in SQL with any data you want rather than being limited to the data in your test tables.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.