Sie sind auf Seite 1von 13

The TRIM function can be used in Oracle/PLSQL.

For example: TRIM(' TRIM(' ' tech from ') ' tech ') would return 'tech' would return 'tech' would return '123' would return 'Tech' would return '23Tech'

TRIM(leading '0' from '000123') TRIM(trailing '1' from 'Tech1') TRIM(both '1' from '123Tech111')

The LTRIM function can be used in Oracle/PLSQL. For example: LTRIM(' LTRIM(' tech'); tech', ' '); would return 'tech' would return 'tech' would return '123' would return 'Tech' would return 'Tech123' would return 'Tech' would return 'Tech'

LTRIM('000123', '0'); LTRIM('123123Tech', '123'); LTRIM('123123Tech123', '123'); LTRIM('xyxzyyyTech', 'xyz'); LTRIM('6372Tech', '0123456789');

The LTRIM function may appear to remove patterns, but this is not the case as demonstrated in the following example. LTRIM('xxyyxzyxyyxTech', 'xyz'); would return 'Tech'

It actually removes the individual occurrences of 'x', 'y', and 'z', as opposed to the pattern of 'xyz'. The LTRIM function can also be used to remove all leading numbers as demonstrated in the next example. LTRIM( '637Tech', '0123456789'); would return 'Tech'

In this example, every number combination from 0 to 9 has been listed in the trim_string parameter. By doing this, it does not matter the order that the numbers appear in string1, all leading numbers will be removed by the LTRIM function. The RTRIM function can be used in Oracle/PLSQL. For example: RTRIM('tech RTRIM('tech '); ', ' '); would return 'tech' would return 'tech' would return '123' would return 'Tech' would return '123Tech'

RTRIM('123000', '0'); RTRIM('Tech123123', '123'); RTRIM('123Tech123', '123');

RTRIM('Techxyxzyyy', 'xyz'); RTRIM('Tech6372', '0123456789');

would return 'Tech' would return 'Tech'

The RTRIM function may appear to remove patterns, but this is not the case as demonstrated in the following example. RTRIM('Techxyxxyzyyyxx', 'xyz'); would return 'Tech'

It actually removes the individual occurrences of 'x', 'y', and 'z', as opposed to the pattern of 'xyz'. The RTRIM function can also be used to remove all trailing numbers as demonstrated in the next example. RTRIM('Tech6372', '0123456789'); would return 'Tech'

In this example, every number combination from 0 to 9 has been listed in the trim_string parameter. By doing this, it does not matter the order that the numbers appear in string1, all trailing numbers will be removed by the RTRIM function. The syntax for the REPLACE function is: REPLACE( string1, string_to_replace, [ replacement_string ] )

string1 is the string to replace a sequence of characters with another set of characters. string_to_replace is the string that will be searched for in string1. replacement_string is optional. All occurrences of string_to_replace will be replaced with replacement_string in string1. If the replacement_string parameter is omitted, the REPLACE function simply removes all occurrences of string_to_replace, and returns the
resulting string.

Applies To
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

For Example
The REPLACE function can be used in Oracle/PLSQL. For example: REPLACE('123123tech', '123'); REPLACE('123tech123', '123'); REPLACE('222tech', '2', '3'); REPLACE('0000123', '0'); would return 'tech' would return 'tech' would return '333tech' would return '123'

REPLACE('0000123', '0', ' ');

would return '

123'

In Oracle/PLSQL, the NVL function lets you substitute a value when a null value is encountered.

Syntax
The syntax for the NVL function is: NVL( string1, replace_with ) string1 is the string to test for a null value. replace_with is the value returned if string1 is null.

Applies To
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

Example #1
The NVL function can be used in Oracle/PLSQL. For example: select NVL(supplier_city, 'n/a') from suppliers; The SQL statement above would return 'n/a' if the supplier_city field contained a null value. Otherwise, it would return the supplier_city value.

Example #2
Another example using the NVL function in Oracle/PLSQL is: select supplier_id, NVL(supplier_desc, supplier_name) from suppliers; This SQL statement would return the supplier_name field if the supplier_desc contained a null value. Otherwise, it would return the supplier_desc.

Example #3
A final example using the NVL function in Oracle/PLSQL is: select NVL(commission, 0) from sales;

This SQL statement would return 0 if the commission field contained a null value. Otherwise, it would return the commission field.

Frequently Asked Questions

Question: I tried to use the NVL function through VB to access Oracle DB.
To be precise, select NVL(Distinct (emp_name),'AAA'),................ from................. I got an oracle error when I use distinct clause with NVL, but when I remove distinct it works fine.

Answer: It is possible to the use the DISTINCT clause with the NVL function. However, the DISTINCT
must come before the use of the NVL function. For example: select distinct NVL(emp_name, 'AAA') from employees; Hope this helps!

Question: Is it possible to use the NVL function with more than one column with the same function call?
To be clear, if i need to apply this NVL function to more than one column like this: NVL(column1;column2 ...... , here is the default value for all )

Answer: You will need to make separate NVL function calls for each column. For example:
select NVL(table_name, 'not found'), NVL(owner, 'not found') from all_tables;

In Oracle/PLSQL, the SUBSTR functions allows you to extract a substring from a string.

Syntax
The syntax for the SUBSTR function is: SUBSTR( string, start_position, [ length ] ) string is the source string. start_position is the position for extraction. The first position in the string is always 1.

length is optional. It is the number of characters to extract. If this parameter is omitted, the SUBSTR function will return the entire string.

Note
If start_position is 0, then the SUBSTR function treats start_position as 1 (ie: the first position in the string). If start_position is a positive number, then the SUBSTR function starts from the beginning of the string. If start_position is a negative number, then the SUBSTR function starts from the end of the string and counts backwards. If length is a negative number, then the SUBSTR function will return a NULL value.

Applies To
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

For Example
The SUBSTR function can be used in Oracle/PLSQL. For example:

SUBSTR('This is a test', 6, 2) SUBSTR('This is a test', 6) SUBSTR('TechOnTheNet', 1, 4) SUBSTR('TechOnTheNet', -3, 3) SUBSTR('TechOnTheNet', -6, 3) SUBSTR('TechOnTheNet', -8, 2)

would return 'is' would return 'is a test' would return 'Tech' would return 'Net' would return 'The' would return 'On'

Starting in Oracle 9i, you can use the CASE statement within an SQL statement. It has the functionality of an IF-THEN-ELSE statement.

Syntax
The syntax for the CASE statement is: CASE [ expression ] WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2 ... WHEN condition_n THEN result_n ELSE result

END expression is optional. It is the value that you are comparing to the list of conditions. (ie: condition_1, condition_2, ... condition_n) condition_1 to condition_n must all be the same datatype. Conditions are evaluated in the order listed. Once a condition is found to be true, the CASE statement will return the result and not evaluate the conditions any further. result_1 to result_n must all be the same datatype. This is the value returned once a condition is found to be true.

Note
If no condition is found to be true, then the CASE statement will return the value in the ELSE clause. If the ELSE clause is omitted and no condition is found to be true, then the CASE statement will return NULL. You can have up to 255 comparisons in a CASE statement. Each WHEN ... THEN clause is considered 2 comparisons.

Applies To
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

For Example
The CASE statement can be used in Oracle/PLSQL. You could use the CASE statement in an SQL statement as follows: (includes the expression clause) select table_name, CASE owner WHEN 'SYS' THEN 'The owner is SYS' WHEN 'SYSTEM' THEN 'The owner is SYSTEM' ELSE 'The owner is another value' END from all_tables; Or you could write the SQL statement using the CASE statement like this: (omits the expression clause) select CASE WHEN WHEN ELSE END table_name, owner='SYS' THEN 'The owner is SYS' owner='SYSTEM' THEN 'The owner is SYSTEM' 'The owner is another value'

from all_tables; The above two CASE statements are equivalent to the following IF-THEN-ELSE statement: IF owner = 'SYS' THEN result := 'The owner is SYS'; ELSIF owner = 'SYSTEM' THEN result := 'The owner is SYSTEM''; ELSE result := 'The owner is another value'; END IF; The CASE statement will compare each owner value, one by one. One thing to note is that the ELSE clause within the CASE statement is optional. You could have omitted it. Let's take a look at the SQL statement above with the ELSE clause omitted. Your SQL statement would look as follows: select table_name, CASE owner WHEN 'SYS' THEN 'The owner is SYS' WHEN 'SYSTEM' THEN 'The owner is SYSTEM' END from all_tables; With the ELSE clause omitted, if no condition was found to be true, the CASE statement would return NULL.

For Example
Here is an example that demonstrates how to use the CASE statement to compare different conditions: select CASE WHEN a < b THEN 'hello' WHEN d < e THEN 'goodbye' END from suppliers;

Frequently Asked Questions

Question: Can you create a CASE statement that evaluates two different fields? I want to return a
value based on the combinations in two different fields.

Answer: Yes, below is an example of a case statement that evaluates two different fields.
select supplier_id, CASE WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office' WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office' END from suppliers; So if supplier_name field is IBM and the supplier_type field is Hardware, then the CASE statement will return North office. If the supplier_name field is IBM and the supplier_type is Software, the CASE statement will return South office. In Oracle/PLSQL, the DECODE function has the functionality of an IF-THEN-ELSE statement.

Syntax
The syntax for the DECODE function is: DECODE( expression , search , result [, search , result]... [, default] ) expression is the value to compare. search is the value that is compared against expression. result is the value returned, if expression is equal to search. default is optional. If no matches are found, the DECODE function will return default. If default is omitted, then the DECODE function will return null (if no matches are found).

Applies To
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

For Example
The DECODE function can be used in Oracle/PLSQL. You could use the DECODE function in an SQL statement as follows: SELECT supplier_name, DECODE(supplier_id, 10000, 'IBM', 10001, 'Microsoft', 10002, 'Hewlett Packard', 'Gateway') result FROM suppliers; The above DECODE statement is equivalent to the following IF-THEN-ELSE statement:

IF supplier_id = 10000 THEN result := 'IBM'; ELSIF supplier_id = 10001 THEN result := 'Microsoft'; ELSIF supplier_id = 10002 THEN result := 'Hewlett Packard'; ELSE result := 'Gateway'; END IF; The DECODE function will compare each supplier_id value, one by one.

Frequently Asked Questions

Question: One of our viewers wanted to know how to use the DECODE function to compare two dates
(ie: date1 and date2), where if date1 > date2, the DECODE function should return date2. Otherwise, theDECODE function should return date1.

Answer: To accomplish this, use the DECODE function as follows:


DECODE((date1 - date2) - abs(date1 - date2), 0, date2, date1) The formula below would equal 0, if date1 is greater than date2: (date1 - date2) - abs(date1 - date2) Helpful Tip: One of our viewers suggested combining the SIGN function with the DECODE function as follows: The date example above could be modified as follows: DECODE(SIGN(date1-date2), 1, date2, date1) The SIGN/DECODE combination is also helpful for numeric comparisons e.g. Sales Bonuses DECODE(SIGN(actual-target), -1, 'NO Bonus for you', 0,'Just made it', 1, 'Congrats, you are a winner')

Question: I would like to know if it's possible to use the DECODE function for ranges of numbers, ie 110 = 'category 1', 11-20 = 'category 2', rather than having to individually decode each number.

Answer: Unfortunately, you can not use the DECODE function for ranges of numbers. However, you
can try to create a formula that will evaluate to one number for a given range, and another number for the next range, and so on. For example: SELECT supplier_id, DECODE(TRUNC ((supplier_id - 1) / 10), 0, 'category 1', 1, 'category 2', 2, 'category 3', 'unknown') result FROM suppliers; In this example, based on the formula: TRUNC ((supplier_id - 1) / 10 The formula will evaluate to 0, if the supplier_id is between 1 and 10. The formula will evaluate to 1, if the supplier_id is between 11 and 20. The formula will evaluate to 2, if the supplier_id is between 21 and 30. and so on...

Question: I need to write a DECODE statement that will return the following:
If yrs_of_service < 1 then return 0.04 If yrs_of_service >= 1 and < 5 then return 0.04 If yrs_of_service > 5 then return 0.06 How can I do this?

Answer: You will need to create a formula that will evaluate to a single number for each one of your
ranges. For example: SELECT emp_name, DECODE(TRUNC (( yrs_of_service + 3) / 4), 0, 0.04, 1, 0.04, 0.06) as perc_value FROM employees;

Question: Is there a limit to the number of arguments that you can have in one DECODE statement? I'm
getting an error, "ORA-00939: too many arguments for function".

Answer: Yes, the maximum number of components that you can have in a DECODE function is 255.
This includes the expression, search, and result arguments.

Oracle/PLSQL: TRUNC Function (with dates)


In Oracle/PLSQL, the TRUNC function returns a date truncated to a specific unit of measure.

Syntax
The syntax for the TRUNC function is: TRUNC ( date, [ format ] ) date is the date to truncate. format is the unit of measure to apply for truncating. If the format parameter is omitted, the TRUNC function will truncate the date to the day value, so that any hours, minutes, or seconds will be truncated off. Below are the valid format parameters:
Unit Year ISO Year Quarter Month Week IW W Day Valid format parameters SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y IYYY, IY, I Q MONTH, MON, MM, RM WW IW W DDD, DD, J

Start day of the week DAY, DY, D Hour HH, HH12, HH24

Minute

MI

Applies To

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

For Example
The TRUNC function can be used in Oracle/PLSQL. For example:
TRUNC(TO_DATE('22-AUG-03'), 'YEAR') TRUNC(TO_DATE('22-AUG-03'), 'Q') TRUNC(TO_DATE('22-AUG-03'), 'MONTH') TRUNC(TO_DATE('22-AUG-03'), 'DDD') TRUNC(TO_DATE('22-AUG-03'), 'DAY') would return '01-JAN-03' would return '01-JUL-03' would return '01-AUG-03' would return '22-AUG-03' would return '17-AUG-03'

Oracle/PLSQL: TRUNC Function (with numbers)


In Oracle/PLSQL, the TRUNC function returns a number truncated to a certain number of decimal places.

Syntax
The syntax for the TRUNC function is: TRUNC( number, [ decimal_places ] ) number is the number to truncate. decimal_places is the number of decimal places to truncate to. This value must be an integer. If this parameter is omitted, the TRUNC function will truncate the number to 0 decimal places.

Applies To
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

For Example
The TRUNC function can be used in Oracle/PLSQL. For example:

TRUNC(125.815) TRUNC(125.815, 0) TRUNC(125.815, 1) TRUNC(125.815, 2) TRUNC(125.815, 3) TRUNC(-125.815, 2) TRUNC(125.815, -1) TRUNC(125.815, -2) TRUNC(125.815, -3)

would return 125 would return 125 would return 125.8 would return 125.81 would return 125.815 would return -125.81 would return 120 would return 100 would return 0

Das könnte Ihnen auch gefallen