Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
all labs.pdf
Скачиваний:
74
Добавлен:
25.02.2016
Размер:
2.04 Mб
Скачать

Using the TO_CHAR Function with Dates

SELECT last_name,

TO_CHAR(hire_date, 'fmDD Month YYYY')

AS HIREDATE

FROM employees;

 

 

 

 

Academy

 

 

 

 

 

 

 

 

 

 

Copyright © 2009, Oracle. All rights reserved.

 

 

Using the TO_CHAR Function with Dates

Oracle

 

 

The SQL statement in the slide displays the last names and hire dat s for all the employees. The hire date appears as 17 June 1987.

Example:

Modify the example in the slide to display the dates in a format that appears as “Seventeenth of June

1987 12:00:00 AM.”

&

Only

 

Internal

SELECT

last name,

 

TO_CHAR(hire date,

 

 

 

Use

 

'fmDdspth "of" Month YYYY fmHH:MI:SS AM')

HIREDATE

 

 

FROM

employees;

 

 

Oracle

 

 

 

Notice that the month follows the format model specified; in other words, the first letter is capitalized and the rest are in lowercase.

Oracle Database 11g: SQL Fundamentals I 4 - 16

Using the TO_CHAR Function with Numbers

TO_CHAR(number, 'format_model')

These are some of the format elements that you can use with the TO_CHAR function to display a number value as a

character:

 

 

Element

 

Result

 

 

 

 

 

 

 

 

 

 

9

 

Represents a number

 

 

 

 

0

 

Forces a zero to be displayed

 

 

 

$

 

Places a floating dollar sign

 

 

 

 

L

 

Uses the floating local currency symbol

 

 

 

.

 

Prints a decimal point

 

 

 

 

,

 

Prints a comma as a thousands indicator

 

 

 

 

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

 

 

 

Using the TO_CHAR Function with Numbers

 

 

 

 

 

 

Oracle

When working with number values, such as character strings, you should convert those numbers to the character data type using the TO_CHAR function, which translates a value of NUMBER data type to VARCHAR2 data type. This technique is especially useful with concatenation.

&

Only

Internal

 

Use

Oracle

 

Oracle Database 11g: SQL Fundamentals I 4 - 17

Using the TO_CHAR Function with Numbers (continued)

Number Format Elements

If you are converting a number to the character data type, you can use the following format elements:

 

Element

Description

 

 

Example

Result

 

 

9

Numeric position (number of 9s determine display

999999

1234

 

 

 

width)

 

 

 

 

 

 

0

Display leading zeros

 

 

099999

001234

 

 

$

Floating dollar sign

 

 

$999999

$1234

 

 

L

Floating local currency symbol

 

 

L999999

FF1234

 

 

D

Returns the decimal character in the specified

99D99

99.99

 

 

 

position. The default is a period (.).

 

 

 

 

 

 

.

Decimal point in position specified

 

 

999999.99

1234.00

 

 

G

Returns the group separator in the specified

9,999

9G999

 

 

 

position. You can specify multiple group

 

 

 

 

 

separators in a number format model.

 

 

 

 

 

 

 

 

 

 

 

 

 

,

Comma in position specified

 

 

999,999

1,234

 

 

MI

Minus signs to right (negative values)

 

999999MI

1234-

 

 

PR

Parenthesize negative numbers

 

 

999999PR

<1234>

 

 

EEEE

Scientific notation (format must specify four Es)

99.999EEEE

1.234E+03

 

 

U

Returns in the specified position the “Euro” (or

U9999

€1234

 

 

 

other) dual currency

 

 

 

 

 

 

 

 

 

 

 

 

V

Multiply by 10 n times (n = number of 9s after V)

9999V99

123400

 

 

 

 

 

 

Academy

 

 

S

Returns the negative or positive value

 

S9999

-1234 or

 

 

 

 

 

 

 

+1234

 

 

B

Display zero values as blank, not 0

 

 

B9999.99

1234.00

 

 

 

 

Oracle

 

 

 

 

&

Only

 

 

 

 

Internal

 

 

 

 

 

 

 

Use

 

 

 

Oracle

 

 

 

 

 

Oracle Database 11g: SQL Fundamentals I 4 - 18

Using the TO_CHAR Function with Numbers

SELECT TO_CHAR(salary, '$99,999.00') SALARY

FROM employees

WHERE last_name = 'Ernst';

 

Copyright © 2009, Oracle. All rights reserved.

Academy

 

 

Using the TO_CHAR Function with Numbers (continued)

 

 

Oracle

• The Oracle server displays a string of number signs (#) in p ac of a whole number whose digits exceed the number of digits provided in the format model.

• The Oracle server rounds the stored decimal value to the number of decimal places provided in

the format model.

 

Only

 

&

Internal

 

Oracle

Use

 

 

Oracle Database 11g: SQL Fundamentals I 4 - 19

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]