Silversleaves
E info@silversleaves.com
T +44 121 288 2824
  • Website Design
  •  ·
  • Commercial Photography

  • Database Development
  •  ·
  • Business Intelligence
  • Home
  • Silversleaves
    • Services
    • Portfolio
  • Web
    • CSS
      • Sticky Footer
      • CSS3 Opacity
    • Firefox Addons
      • DGSCheck
  • Database
    • Oracle Certification
      • SQL Fundamentals I - Part 1
        • Introduction
        • Relational Theory
        • DESCRIBE
      • SQL Fundamentals I - Part 2
        • SQL Operators & Expressions
        • WHERE
        • ORDER BY
        • Ampersand Substitution
      • SQL Fundamentals I - Part 3
        • Single Row Functions
        • Conversion Functions
        • Group Functions
      • SQL Fundamentals I - Part 4
        • Joining Tables
        • Subqueries
        • Set Operators
        • DML
        • Transactions
      • SQL Fundamentals I - Part 5
        • Manipulating Tables
        • Schema Objects
  • Business Intelligence
    • Coming Soon
  • Photography
    • Food
      • Sweet
      • Savory
    • Drink
      • Liquids
    • Interior
      • Hotel
  • Graphic Design
    • Branding
      • Logos
    • Print
      • Poster
      • Brochure
      • Advert
  • Partners
    • Élite Hotels, Turkey
    • Terzioğlu İnsaat, Turkey
  • Blog

Oracle Certification - SQL Fundamentals I - Conversion Functions

  • Implicit
  • Explicit
  • Common
  • NULLs
  • Conditional
  • Summary

Implicit Conversions

There are occasions when data is passed to functions in the incorrect format. Where possible Oracle will perform an implicit conversion from one data-type to another according to the format that a function expects.

The function LENGTH takes a character string and returns a numeric length. Here the number 123456789 has been implicitly converted into a character string:

In this example LENGTH is supplied with the current system date, the DATE is implicitly converted into a character string using the default date format (DD-MON-RR):

Explicit Conversions

Explicit conversions between data-types are very important when manipulating data in the database. The OCP exam covers their usage, and you will be expected to know how they work (i.e. understand the format masks that they use), and be able to predict the output they will generate. Typically a nested set of conversion functions will be tested, i.e.

Common Conversion Functions

The following are the more commonly used conversion functions.

TO_CHAR(n,[fmt],[nlsparam])

TO_CHAR converts number n to a VARCHAR2 using the optional format fmt. When the format mask is smaller than the number being converted a string of hashes is returned.

Element Example Desc Input Result
9 9999 Numeric width 99 ' 99'
0 0099 Leading zeros 99 '0099'
. 099.99 Period 99.99
D 099D99 NLS Decimal Point 99.99 '099.99'
, 999,999 Comma position 1234 '1,234'
G 999G999 NLS group separator1234 '1,234'
$ $99.99 Dollar Sign99.99 '$99.99'
L L99.99 Local Currency99.99 '$99.99'
MI 99.99MI Minus Sign-99.99 '99.99-'
S S99.99 + or - sign99.99 '+99.99'

TO_NUMBER(string,[fmt],[nlsparam])

TO_NUMBER converts string to a NUMBER using the optional format fmt. See the format masks above.


TO_CHAR(d,[fmt],[nlsparam])

TO_CHAR converts date d to a VARCHAR2 using the optional format fmt. In this case the format model is case sensitive. The names of days and months are automatically padded unless fmt is pre-fixed with 'fm', in which case all white-space is trimmed.

Element Desc Input Result
Y Last digit of the year 01-JAN-2011 '1'
YY Last two digits of the year 01-JAN-2011 '11'
YYY Last three digits of the year 01-JAN-2011 '111'
YYYY Four digit year 01-JAN-2011 '2011'
YEAR English spelling 01-JAN-2011 'TWENTY ELEVEN'
RR 2 digit year01-JAN-2011 '11'
MM Two digit month01-JAN-2011 '01'
MON Three letter month01-JAN-2011 'JAN'
MONTH English month01-JAN-2011 'JANUARY'
D Day of the week01-JAN-2011 '6'
DD Two digit day of the month01-JAN-2011 '01'
DDD Day of the year01-JAN-2011 '001'
DY Three letter day01-JAN-2011 'SAT'
DAY English day of the week01-JAN-2011 'SATURDAY'

The time portion of the date can be extracted with the following elements:

Element Desc Input Result
AM, A.M. Meridian 01-JAN-2011 23:59:59 'PM'
PM, P.M Meridian 01-JAN-2011 23:59:59 'PM'
HH, HH12 Hour of the day 1-12 01-JAN-2011 23:59:59 '11'
HH24 Hour of the day 0-23 01-JAN-2011 23:59:59 '23'
MI Minute 0-59 01-JAN-2011 23:59:59 '59'
SS Second 0-5901-JAN-2011 23:59:59 '59'
SSSS Seconds past midnight01-JAN-2011 23:59:59 '86399'

TO_DATE(string,[fmt],[nlsparam])

TO_DATE converts string to a DATE using the optional format fmt. See the format masks above.

NULL Conversion Functions

NVL(expr,ifnull)

The NVL function evaluates expr, if it is NULL it returns ifnull, otherwise it returns expr. The two parameters must be of the same type, or it must be possible for oracle to convert them:


NVL2(expr,ifnotnull,ifnull)

The NVL2 function is almost identical to NVL, apart from returning ifnotnull if expr is not null.

Note: Pay attention to the order of the parameters in NVL2 vs NVL it is easy to confuse the two. With NVL2 it is the 3rd parameter that is returned when the expression is null, not the second.


NULLIF(expr1,expr2)

NULLIF compares two expressions, if they are equal it returns NULL, otherwise expr1.


COALESCE(expr1,expr2...exprn)

COALESCE returns the first non-null value from it's parameter list, otherwise it returns NULL.

Conditional Functions

DECODE(expr1,comp1,iftrue1,[comp2,iftrue2...],[iffalse])

The DECODE function is like an if-then-else statement.

The CASE Expression

CASE expr
 WHEN comp1 THEN iftrue1
 [WHEN comp2 THEN iftrue2
 ...
 WHEN compn THEN iftruen
 ELSE iffalse]
END

Summary

There is going to close attention paid to all of these functions, both on their syntax and operation.

Moving on, Group Functions.

<< Previous Topic
Next Topic >>
© Silversleaves Limited 2012