Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Wednesday, January 1, 2014

Oracle String Comparison

There are two basic rules used compare character values in Oracle.

Blank-padded Comparison
Blank-padded comparison is used with CHAR, NCHAR, text literals or values returned by the USER function.  Here if two values have different lengths, then blanks are added to the shorter one such that their lengths are equal.  Then values are compared character by character up to the first character that differs. The value with the greater character at the first differing position is considered as greater.

Non-padded Comparison

This is used with VARCHAR2 or NVARCHAR2. Here no padding is used. Instead oracle compares two values character by character up to the first character that differs.  The value with the greater character in that position is considered greater. If the lengths of the two comparing values are different and the values are identical up to the end of the shorter longer value is considered greater.

Here I have mentioned a small example on how blank-padded comparison and non-padded comparison will behave when comparing two string values.

Comparing 'a ' and 'a'

'a ' = 'a' is TRUE with blank-padded comparison 


 but with non-padded comparison it is FALSE




 'a ' > 'a' is FALSE with blank-padded comparison


 but with non-padded comparison it is TRUE


Tuesday, November 5, 2013

Oracle TRUNC Function

TRUNC(number) function can take two arguments.
TRUNC(param1,param2)

Here the function will return param1 truncated by param2 decimal places.










If param2 is omitted the function will return param1 truncated by 0 decimal places.










param2 can be negative as well. This will truncate the digits left of the decimal point.










The general behavior of TRUNC function is as below.

TRUNC(n,m) is

TRUNC(n,m) = TRUNC(n * 10 ^ m) * 10 ^ (-m)

In the above case, where m = -2

TRUNC(123.456,-2) = TRUNC(123.456 * 10 ^ (-2)) * 10 ^ (2)
                                  = TRUNC(1.23456) * 100
                                  = 1 * 100
                                  = 100

This function can take any numeric data type as an argument or any non-numeric data type that can be implicitly converted to a numeric data type.