Imgt Finals
Imgt Finals
SQL Functions
LEN
Single-Row Functions - Returns the number of characters of the
- Manipulate data items specified string expression.
- Accept arguments and return one value
- Act on each row returned
- Return one result per row
- May modify the datatype
- Can be nested
LEFT
Single Functions - Returns the part of a character string starting at
a specified number of characters from the left.
RIGHT - Replaces all occurrences of the second given
- Returns the part of a character string starting at string expression in the first string expression
a specified number of characters from the right. with a third expression.
LTRIM
- Returns a character expression after removing
leading blanks.
REPLICATE
- Repeats a character expression for a specified
number of times.
RTRIM
- Returns a character expression after removing SUBSTRING
trailing spaces. - Returns part of a character, binary, text or image
expression
REPLACE
Mathematical Functions
CHARINDEX
- Returns the starting position of a string specify.
Mathematical Functions
DATEDIFF
- Returns the number of date and time boundaries
crossed between two specified dates
- Sometimes we only want a portion of a date or
we want to do date arithmetic
GETDATE
- Returns the current system date and time in the
Microsoft SQL Server standard internal format
for datetime values.
DATENAME
- Returns a character string representing the
specified datepart of the specified date.
DATEADD
- Returns a new datetime value based on adding
an interval to the specified date.
DATEPART
- Returns an integer representing the specified
datepart of the specified date,
CAST
Conversion Functions
CONVERT
data_type
- Is the target system-supplied data type
CAST and CONVERT
length
- CAST and CONVERT provide similar
- Is an optional parameter of nchar, nvarchar,
functionality,
char, varchar, binary, or varbinary data types.
- Explicitly converts an expression of one data
type to another.
expression
- Is any valid Microsoft SQL Server expression.
CAST
style
- Is the style of date format used to convert
datetime or smalldatetime data to character
Expression
data.
- Is any valid Microsoft SQL Server
expression
Data_type
- Is the target system-supplied data type.
Types of Joins
General Functions
What is an Equijoin?
What is a Join?
Outer Joins
1. The Orders table for the “purchased” - An outer join includes not only rows from the two
2. The [Order Detail] table for the “purchased” data sources with a match but also unmatched
3. The Product table for the “Products” rows from outside the intersection.
- An outer join extends the inner join by adding
the nonmatching data from the left or right data
set.
- Some of the data in the result set produced by
an outer join will look just like the data from an
inner join.
- Any rows from the outer-join table that do not
have a match in the other side of the join will
return data only from the outer-join table. In this
case, columns from the other data source will
have null values.
- Outer joins are often employed when a
secondary table has a foreign-key constraint to
the primary table and permits nulls in the foreign
key.
Cartesian Product
A Cartesian product is formed when:
- A join condition is omitted.
- A join condition is invalid
Customers that did not place orders: - All rows in the first table are joined to all rows in
the second table.
Self Joins
- A self-join is a join that refers back to the same
table.
WEEK 16 : VIEWS
Views
- Virtual table whose contents are defined by a
query.
- based on the result set of a SELECT statement.
- Contains rows and columns just like a real table
- The rows and columns of data come from tables
referenced in the query defining the view and
are produced dynamically when the view is
referenced.
- you can add TSQL arguments to a view
definition such as SELECT, WHERE, JOIN to
- To find the name of each employee’s manager, present the data as if the data was coming from
you need to join the EMPLOYEES table to itself, a single table.
or perform a self join. - A view can be used inside a query, a stored
procedure or another view.
- Restrict a user to specific rows in a table.
- Restrict a user to specific columns.
- Join columns from multiple tables so that they
look like a single table.
- Aggregate information instead of supplying
details.
Simple Stored Procedure
Referencing Views
Modifying View
Stored Procedure
- Collection of SQL statements.
- Can accept parameters
- Contain programming statements that perform
operations in the database, including calling
other procedures.
Stored Procedure Execution
Creating a Stored Procedure
AS
- Specifies the actions the procedure is to take.
Sql_statement
- Is any number and type of Transact-SQL
statements to be included in the procedure.
Altering Stored Procedure
- Alters a previously created procedure, created
by executing the CREATE PROCEDURE
statement, without changing permissions and
without affecting any dependent stored
procedures.
Dynamic SQL
- Dynamic SQL is T-SQL code assembled into a
character string, interpreted as a command and
executed.
- Dynamic SQL provides flexibility for
administrative, programming tasks.
Using sp_executesql
- Accepts string as code to be run
- Allows parameterized code without risk of SQL
injection
- Can perform better than EXEC due to query plan
reuse