Professional Documents
Culture Documents
MASAKA CAMPUS
DATABASE PROGRAMMING COURSE WORK
BSC IT 2 2018 GROUP 3.MEMBERS
GROUP.3 REGISTRATION NUMBERS
MULAALA GODFREY
2016-B072-30016
KAYIWA EUGINE
MUTUMBA 2016-B072-30004
KAMAHORO KEVINA 2016-B072-30034
Implicit conversions are not visible to the user. SQL Server automatically converts the data
from one data type to another. For example, when a smallint is compared to an int,
the smallint is implicitly converted to int before the comparison proceeds.
Use CAST instead of CONVERT if you want Transact-SQL program code to comply with ISO. Use
CONVERT instead of CAST to take advantage of the style functionality in CONVERT.
Some implicit and explicit data type conversions are not supported when you are converting
the data type of one SQL Server object to another. For example, an nchar value cannot be
converted to an image value. An nchar can only be converted to binary by using explicit
conversion, an implicit conversion to binary is not supported. However, an nchar can be
explicitly or implicitly converted to nvarchar.
To_char,To_date,To_number
Explicit data type conversion
SQL provided 3 functions to convert a value from one data type to another.
To_char(number conversion)
to_char(number,fmt)
Formatting Functions
Return
Function Description Example
Type
convert time stamp to to_char(current_timestamp,
to_char(timestamp, text) text
string 'HH12:MI:SS')
to_char(interval '15h 2m 12s',
to_char(interval, text) text convert interval to string
'HH24:MI:SS')
to_date('05 Dec 2000',
to_date(text, text) date convert string to date
'DD Mon YYYY')
Nesting functions
Nesting is the act of using a function as one of the arguments in a formula that uses a function.
We’ll refer to that function as a nested function. For example, by nesting the AVERAGE and
SUM function in the arguments of the IF function, the following formula sums a set of numbers
(G2:G5) only if the average of another set of numbers (F2:F5) is greater than 50. Otherwise, it
returns 0.
The AVERAGE and SUM functions are nested within the IF function.
OUT PUT
EmployeeId DepartmentId Salary
1 1 10
2 1 30
3 2 30
4 2 40
5 2 20
6 3 40
7 3 50
after grouping
DepartmentId AVG(Salary)
1 (10+30)/2 = 20
2 (30+40+20)/3 = 30
3 (40+50)/2= 45
So the query below will return 45 as Maximum average salary for departmentId 3
NVL Function
The syntax of NVL function is
NVL(expr1, expr2)
The NVL function takes two arguments as its input. If the first argument is NULL, then it returns
the second argument otherwise it returns the first argument.
Example
NVL2 Function
The syntax of NVL2 function is
The NVL2 function takes three arguments as its input. If the expr1 is NOT NULL, NVL2 function
returns expr2. If expr1 is NULL, then NVL2 returns expr3.
The NULLIF function compares the two expressions and returns NULL if they are equal
otherwise it returns the first expression.
Example
COALESCE Function
The Syntax of COALESCE function is
The COALESCE function takes N number of arguments as its input and returns the first NON-
NULL argument.
Conditional expressions
In computer science, conditional statements, conditional expressions and conditional
constructs are features of a programming language, which perform different computations or
actions depending on whether a programmer-specified Boolean condition evaluates to true or
false. Apart from the case of branch predication, this is always achieved by selectively altering
the control flow based on some condition.
In imperative programming languages, the term "conditional statement" is usually used,
whereas in functional programming, the terms "conditional expression" or "conditional
construct" are preferred, because these terms all have distinct meanings.
A conditional is sometimes colloquially referred to as an "if-check," especially when perceived
as a simple one and when its specific form is irrelevant or unknown.
CASE statement
2. Or Case Search where a value expression is employed to determine the branch used.
Syntax 1
CASE WHEN condition THEN result
[WHEN ...]
[ELSE result]
END
Syntax 2
CASE valueExpression WHEN value1 THEN result1
[WHEN ...]
[ELSE result]
END
Description
True, then the value of the CASE expression is the result that follows the condition.
If no WHEN condition is true then the value of the case expression is the result in
the ELSE clause.
CASE allows you to perform IF-THEN-ELSE logic in your SQL statements, similar to DECODE.
CASE [expression] WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2... WHEN
condition THEN result_n ELSE result END case_name
CASE offers more flexibility than the DECODE function. Tasks that are hard to implement with
DECODE are easy to implement using CASE, which makes it easier to write your SQL.
It's also easier to read. More keywords within the statement allow you to break up the logic,
rather than using a series of parameters in a single function.
They also handle NULL values differently. With DECODE, NULL is equal to NULL. With CASE,
NULL is not equal to NULL.
The DECODE function is an older function, but still quite powerful. It can handle advanced logic,
but can get hard to read as the function gets longer.