Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

173


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

I


magine you had a large pile of LEGO bricks that all fi t each other. You could construct elaborate
buildings, boats, trucks, and so on. If you do a quick search on the Internet, you can fi nd large
and complex LEGO constructions. The interconnectivity of this unique set of LEGOs makes them
fl exible. In the same way, the interconnectivity of SQL expressions and functions makes Transact-
SQL (T-SQL) one of the most fl exible 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 que-
ries, wielding expressions and scalar functions are defi nitely 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.

Data Types


Now you may be wondering, data types, isn’t this section all about expressions and functions? You
are correct. However, before discussing those important aspects of T-SQL, here’s a quick overview

c08.indd 173c08.indd 173 7/30/2012 4:21:09 PM7/30/2012 4:21:09 PM


http://www.it-ebooks.info
Free download pdf