Chapter 8

Data Types, Expressions, and Scalar Functions

In This Chapter

Data Types

Working with Expressions and Scalar Functions

Using Logic Within a Query

Working with Nulls, Strings, and Dates

Imagine you had a large pile of LEGO bricks that all fit each other. You could construct elaborate buildings, boats, trucks, and so on. If you do a quick search on the Internet, you can find large and complex LEGO constructions. The interconnectivity of this unique set of LEGOs makes them flexible. In the same way, the interconnectivity of SQL expressions and functions makes Transact- SQL (T-SQL) one of the most flexible and powerful database querying languages.

Expressions can retrieve data from a subquery, handle complex logic, convert data types, and manipulate data. If the secret to being a competent SQL database developer is mastering SQL queries, wielding expressions and scalar functions are definitely in the arsenal.

An expression is any combination of constants, functions, or formulas that returns a single value. Expressions may be as simple as a hard-coded number, or as complex as a case expression that includes several formulas and functions.

Expressions may be employed in several places within the SQL syntax. Nearly anywhere a value may be used, an expression may be used instead. This includes column values, JOIN ON clauses, WHERE and HAVING clauses, and ORDER BY columns. Expressions can't be substituted for object names, such as table names or column names.

Get Microsoft SQL Server 2012 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.