SQL Server Basics Part One
SQL Server Basics Part One
OPERATORS
• Arithmetic operators
• Assignment operator
• Bitwise operators
• Comparison operators
• Logical operators
• Unary operators
Operator Meaning
+ (Add) Addition
- Subtraction.
* Multiplication.
/ (Divide) Division
% Returns the integer remainder of a division. For
example, 12 % 5 = 2 because the
remainder of 12 divided by 5 is 2.
The plus (+) and minus (-) can also be used to perform arithmetic
operations on datetime and smalldatetime values.
2 Assignment operator Transact-SQL has one assignment operator, the equals sign
(=). In this example, the @MyCounter variable is
created. Then, the assignment operator sets
@MyCounter to a value returned by an expression.
USE Northwind
GO
SELECT FirstColumnHeading = 'xyz',
SecondColumnHeading = ProductID
FROM Products
GO
Operator Meaning
& (Bitwise AND) Bitwise AND (two operands).
| (Bitwise OR) Bitwise OR (two operands).
^ (Bitwise Bitwise exclusive OR (two operands).
Exclusi
ve OR)
The operands for bitwise operators can be any of the data
types of the integer or binary string data type
categories (except for the image data type), with
the exception that both operands cannot be any
of the data types of the binary string data type
category. The table shows the supported
operand data types.
Left operand Right operand
binary int, smallint, or tinyint
bit int, smallint, tinyint, or bit
int int, smallint, tinyint, binary, or
varbinary
smallint int, smallint, tinyint, binary, or varbinary
tinyint int, smallint, tinyint, binary, or varbinary
varbinary int, smallint, or tinyint
4 Comparison operators Comparison operators test whether or not two expressions are
the same. Comparison operators can be used on all
expressions except expressions of the text, ntext, or
image data types.
Operator Meaning
= (Equals) Equal to
> (Greater Than) Greater than
< (Less Than) Less than
>= (Greater Than or Greater than or equal to
Equal
To)
<= (Less Than or Less than or equal to
Equal
To)
<> (Not Equal To) Not equal to
!= (Not Equal To) Not equal to (not SQL-92 standard)
!< (Not Less Not less than (not SQL-92 standard)
Than)
!> (Not Greater Not greater than (not SQL-92
Than) standard)
The result of a comparison operator has the Boolean data
type, which has three values: TRUE, FALSE, and
UNKNOWN. Expressions that return a Boolean
data type are known as Boolean expressions.
Unlike other SQL Server data types, a Boolean data
type cannot be specified as the data type of a table
column or variable, and cannot be returned in a
result set.
When SET ANSI_NULLS is ON, an operator that has one or two NULL
expressions returns UNKNOWN. When SET ANSI_NULLS is
OFF, the same rules apply, except an equals operator returns
TRUE if both expressions are NULL. For example, NULL =
NULL returns TRUE if SET ANSI_NULLS is OFF.
USE Northwind
GO
DECLARE @MyProduct int
SET @MyProduct = 10
IF (@MyProduct <> 0)
SELECT *
FROM Products
WHERE ProductID = @MyProduct
GO
5 Logical operators Logical operators test for the truth of some condition. Logical
operators, like comparison operators, return a
Boolean data type with a value of TRUE or FALSE.
Operator Meaning
ALL TRUE if all of a set of comparisons are
TRUE
AND TRUE if both Boolean expressions are
TRUE.
ANY TRUE if any one of a set of comparisons
are TRUE.
BETWEEN TRUE if the operand is within a range
EXISTS TRUE if a subquery contains any rows.
IN TRUE if the operand is equal to one of a
list of expressions.
LIKE TRUE if the operand matches a pattern.
NOT Reverses the value of any other Boolean
operator.
OR TRUE if either Boolean expression is
TRUE.)
SOME TRUE if some of a set of comparisons
are TRUE.
6 String concatenation The string concatenation operator allows string concatenation
operator with the addition sign (+), which is also known as
the string concatenation operator. All other string
manipulation is handled through string functions
such as SUBSTRING.
Operator Meaning
+ (Positive) Numeric value is positive.
- (Negative) Numeric value is negative.
~ (Bitwise NOT) Returns the ones complement of the
number.
The + (Positive) and - (Negative) operators can be used on
any expression of any of the data types of the
numeric data type category. The ~ (Bitwise NOT)
operator can be used only on expressions of any of
the data types of the integer data type category.
OPERATOR PRECEDENCE
When a complex expression has multiple operators, operator precedence determines the sequence in
which the operations are performed. The order of execution can significantly affect the resulting value.
Operators have these precedence levels. An operator on higher levels is evaluated before an
operator on a lower level:
• =, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)
• NOT
• AND
• = (Assignment)
When two operators in an expression have the same operator precedence level, they are evaluated
left to right based on their position in the expression. For example, in the expression used in the SET
statement of this example, the subtraction operator is evaluated before the addition operator.
FUNCTIONS
Functions in programming languages are subroutines used to encapsulate frequently performed logic.
Any code that must perform the logic incorporated in a function can call the function rather than having to
repeat all of the function logic
Built-in Functions:
Operate as defined in the Transact-SQL Reference and cannot be modified. The functions can be
referenced only in Transact-SQL statements using the syntax defined in the Transact-SQL Reference.
Microsoft® SQL Server™ 2000 has built-in functions to perform certain operations. The
function categories are:
User-defined functions :
Allows us to define our own Transact-SQL functions using the CREATE FUNCTION statement.
User-defined functions take zero or more input parameters, and return a single
value. Some user-defined functions return a single, scalar data value, such as an
int, char, or decimal value.
For example, this statement creates a simple function that returns a decimal:
• Scalar functions
A user-defined function takes zero or more input parameters and returns either a scalar value or a
table. A function can have a maximum of 1024 input parameters. When a parameter of the function
has a default value, the keyword default DEFAULT must be specified when calling the function to
get the default value. This behavior is different from parameters with default values in stored
procedures in which omitting the parameter also implies the default value. User-defined functions
do not support output parameters.