You are on page 1of 9

UGANDA MARTYRS UNIVERSITY

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

NAKATO VICTORIA 2016-B072-30035

MUKUNDANE JOSHUA 2016-B072-30015


Implicit and explicit conversion

Data types can be converted either implicitly or explicitly.

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.

GETDATE() implicitly converts to date style 0. SYSDATETIME() implicitly converts to date style


21.

Explicit conversions use the CAST or CONVERT functions.

The CAST and CONVERT functions convert a value (a local variable, a column, or another


expression) from one data type to another. For example, the following CAST function converts
the numeric value of $157.27 into a character string of '157.27':
SQLCopy

CAST ( $157.27 AS VARCHAR(10) )

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.

The explicit conversions are

TO_CHAR-->To character conversion


TO_DATE-->To date conversion
TO_NUMBER-->To number conversion
TO_CHAR conversion
This function can be used in 2 ways

    to_char(number conversion)

    to_char(date conversion)

To_char(number conversion)
to_char(number,fmt)

Converts number datatype to a value of varchar2 datatype.


'fmt' is the optional number format,that can be used.

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_char(int, text) text convert integer to string to_char(125, '999')

to_char(double convert real/double


text to_char(125.8::real, '999D9')
precision,text) precision to string

to_char(numeric, text) text convert numeric to string to_char(-125.8, '999D99S')

to_date('05 Dec 2000',
to_date(text, text) date convert string to date
'DD Mon YYYY')

to_number(text, text) numeric convert string to numeric to_number('12,454.8-', '99G999D9S')

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.

You can nest up to 64 levels of functions in a formula.

In computer programming, a nested function (or nested procedure or subroutine) is


a function which is defined within another function, the enclosing function. Due to simple
recursive scope rules, a nested function is itself invisible outside of its immediately enclosing
function, but can see (access) all local objects (data, functions, types, etc.) of its immediately
enclosing function as well as of any function(s) which, in turn, encloses that function. The
nesting is theoretically possible to unlimited depth, although only a few levels are normally
used in practical programs.
Example in SQL
Select MAX (AVG (salary)) from employees group by department_id;

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

SELECT MAX (x.avg)


FROM (SELECT AVG (salary) as avg FROM employees group by department_id) x;
General Functions
The general functions work with any data type and are mainly used to handle null values. The
Oracle general functions are

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

SELECT NVL (10,2) FROM DUAL;

SELECT NVL (NULL, ‘Oracle') FROM DUAL;

SELECT NVL (NULL, NULL) FROM DUAL;

NVL2 Function
The syntax of NVL2 function is

NVL2 (expr1, expr2, expr3)

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.

SELECT NVL2 ('Ora','SID','TNS') FROM DUAL;

SELECT NVL2 (NULL,'SID','TNS') FROM DUAL;


NULLIF Function
The Syntax of NULLIF function is

NULLIF (expr1, expr2)

The NULLIF function compares the two expressions and returns NULL if they are equal
otherwise it returns the first expression.

Example

SELECT NULLIF('Oracle','MYSQL') FROM DUAL;

SELECT NULLIF('MYSQL','MYSQL') FROM DUAL;

COALESCE Function
The Syntax of COALESCE function is

COALESCE (expr1, expr2, expr3...)

The COALESCE function takes N number of arguments as its input and returns the first NON-
NULL argument.

SELECT COALESCE ('DB Backup’, ‘Oracle') FROM DUAL;

SELECT COALESCE (NULL,'MYSQL', NULL) FROM DUAL;

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

There are two key uses for the CASE expression:

1. A more generic version where every condition should be a boolean expression


evaluated on true or false, and the first evaluated to true determines the result.

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

CASE clauses can be used wherever an expression is valid. condition is an expression that


returns a boolean result.

If the result is:

 True, then the value of the CASE expression is the result that follows the condition.

 False, any subsequent WHEN clauses are searched in the same manner.

If no WHEN condition is true then the value of the case expression is the result in
the ELSE clause.

If the ELSE clause is omitted and no condition matches, the result is NULL.


The CASE statement in Oracle isn't a function, so we haven't labelled it as one.

CASE allows you to perform IF-THEN-ELSE logic in your SQL statements, similar to DECODE.

The syntax is:

CASE [expression] WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2... WHEN
condition THEN result_n ELSE result END case_name

The expression is used to compare against. Many conditions and results can be specified, and if


a condition matches the expression, then the result is returned. The ELSE keyword specifies
what happens if no condition is met.

It was introduced into Oracle to replace the DECODE function.

Why have both then?

Well, there are several reasons.

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


The DECODE function in Oracle allows you to have IF-THEN-ELSE logic in your SQL statements.

The syntax is:

DECODE (expression, search, result [, search, result]... [, default])

The expression is the value to compare. Many combinations of search and result can be


supplied. Search is compared against the expression, and if it is true, then result is returned.

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.

You might also like