Share this Page URL
Help

Chapter 27. SQL Primer > Operators - Pg. 539

SQL Primer 539 Expression If you are the least bit familiar with programming languages, you know that expressions are anything that, when calculated, result in a value. For instance, 1 + 1 = 2 is an example of an expression. Expressions in SQL work much the same way. Consider the following data from the customers table: CustomerID 1 2 3 4 FirstName Zak David Matthew Jessica LastName Ruvalcaba Levinson Pizzi Ruvalcaba You could use a simple Select statement to display the information exactly as it appears in the preceding table, or you could write an expression that concatenates the FirstName and LastName fields. The query would look like this: Select CustomerID, FirstName & LastName As Name From Customers Notice the "&" operator. The "&" operator is used to concatenate or join together two fields into one virtual field using the As keyword. The results would display as follows: CustomerID 1 2 3 4 Name ZakRuvalcaba DavidLevinson MatthewPizzi JessicaRuvalcaba Notice that there is no space between the first and last names. To add a space, you need to add a literal string value: Select CustomerID, FirstName & ' ' & LastName As Name From Customers Adding the space results in spaces between the first and last names: CustomerID 1 2 3 4 Name Zak Ruvalcaba David Levinson Matthew Pizzi Jessica Ruvalcaba Operators In the previous section, you were introduced to the use of the "&" operator. Operators are used in programming languages to aid in the evaluation of expressions. The following table lists operators that you should be familiar with: * / ­ > The multiplication operator is used when multiplying fields or values. The divide operator is used when dividing fields or values. The minus operator is used when subtracting fields or values. The greater-than operator is used in Where clauses to determine whether a first value is greater than the second, such as Select * From Customers Where CustomerID > 10. The result would return all the CustomerIDs after 10.