Skip to content

SQL 101 – Calculated Fields

2005 September 5
tags: , ,
by Shannon Lowder

When working with SQL you will inevitably be asked to return data from your database in a way it’s not stored.  You’ll be asked to return city, state and zip as a single string, yet they are stored separately.  You’ll be asked to return grand totals, but you only have line item totals.  You’ll be asked to provide counts, totals, or averages, and none of that is in the database.

The answer to all these scenarios is to calculate those values for the user.

Concatenating Fields

Let’s say we have an address table, and the columns city, state, and zip are each stored in there as VARCHAR.  It’s important that all three are varchar, otherwise you’ll get errors when you try to combine them all into one string.  Given this table, you want to present a list of “cityName, state zipcode”.  Note the spacing and the comma in the string.  How would we do this?

SELECT
   city + ', ' + state + ' ' + zip AS [output]
FROM address

output
------
Charlotte, NC 28222
Kannapolis, NC 28081
Beverly Hills, CA 90210

Concatenating VARCHAR values is simple.  Just remember all the fields have to be VARCHARs before concatenation.  If one or more fields aren’t, then you can CONVERT them to VARCHAR first.

Mathematics

Let’s say you have a table of sales, and in that table it lists, productName, quantitySold, and price.  How could you query this table to add a subtotal column that shows the price paid for that quantity sold?

SELECT
     productName
   , quantitySold
   , price
   , quantitySold * price AS subTotal
FROM sales

prodctName   quantitySold   price   subtotal
----------   ------------   -----   --------
pen          1              .99     .99
paper        25             1.00    25.00

You can use any mathematical operator in the place of *.  Basically any time you compute a column, the query will look like a formula, keep that in mind, and this will be a breeze!

Conclusion

This is only the beginning of creating calculated fields in SQL.  In letter posts I’l show you extra functions that can do far more than simple math.  After that I’ll show you aggregate functions that can sum several rows of information into one row.

If you have any questions, please send them in, I’m working hard to help explain the fundamentals of SQL so you can become better equipped to work through the many questions you’ll be asked one day.  I can only help you, if you “help me, help you.”

Previous: Advanced LIKE clauses Next: Summarizing Data
6 Responses leave one →
  1. brackmr permalink
    November 4, 2005

    Excellent introduction to Sql. This is EXACTLY what I was looking for

  2. thefha permalink
    November 4, 2005

    Not only for beginners tho!

  3. seojobshut permalink
    November 4, 2005

    Thanks for providing such kind of priceless information.

Trackbacks and Pingbacks

  1. SQL 101 – Summarizing Data | Shannon Lowder
  2. SQL 101 | Shannon Lowder
  3. SQL 101 – The Advanced LIKE Clauses | Shannon Lowder

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS