0% found this document useful (0 votes)
6 views

lectia8_EXEMPLE

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lectia8_EXEMPLE

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

SINGLE ROW

FUNCTIONS
1. CHARACTER MANIPULATION
Concatenation
 str1 || str2
 CONCAT(str1,str2)

 To concatenate more that 2 strings:


 Str1 || str2 || str4
 CONCAT(str1,CONCAT(str2,str3))
RPAD, LPAD
 RPAD(string,length,['set'])
 LPAD(string,length,['set'])

If 'set' does not appear the string will be padded


with spaces.
RPAD, LPAD Examples
SELECT RPAD('ab',10,'<>') FROM DUAL
=> ab<><><><>

SELECT LPAD('xyz',15,'*') FROM DUAL


 ************xyz
RPAD, LPAD Examples
SELECT
LPAD(RPAD('abc',8,'='),13,'=')
FROM DUAL

=> =====abc=====
LTRIM, RTRIM
 Trim off unwanted characters from the left
(LTRIM), right (RTRIM)
 LTRIM(string,[’set’])
 LTRIM(string,[’set’])

 Set is the collection of characters you want to


trim off. If no set is specified, the function trim
off spaces.
LTRIM, RTRIM Examples
SELECT LTRIM('**==**abc**==**','*')
FROM DUAL

=> ==**abc**==**

SELECT RTRIM('**==**abc**==**','*')
FROM DUAL

=> **==**abc**==
TRIM
 trim ( [leading|trailing|both
['set'] FROM] s1)

 Trim off set from the beginning, the end or both of


the string s1
 both option is Implicit
 If no set is specified, the function trim off spaces.
TRIM Examples
SELECT TRIM(leading 'ab' from 'ababbabbabab')
FROM DUAL

=> babababab
TRIM Examples
SELECT TRIM(trailing 'ab' from 'ababbabbabab')
FROM DUAL

=> ababbabb
TRIM Examples
SELECT TRIM('ab' from 'ababbabbabab')
FROM DUAL

=> babb
LOWER, UPPER, INITCAP
 UPPER(string)
 LOWER(string)
 INITCAP(string)
LOWER, UPPER, INITCAP
SELECT UPPER('aBcD'), LOWER('aBcD'),
INITCAP('aBcD') FROM DUAL

=> ABCD abcd Abcd


LENGTH
SELECT LENGTH('abc') FROM DUAL

=> 3
SUBSTR
SUBSTR(string,start[,count])
- If no count is specified, the function return the substring
starting at position start and going to the end of the
string.

SELECT SUBSTR('abcdefghi',5,2) FROM DUAL


 ef

SELECT SUBSTR('abcdefghi',5) FROM DUAL


 efghi
INSTR
INSTR(string,substring[,start[,n]])
 Search for the n-th occurrence of the substring in

the string, starting with the start position


 If the start position is not mention the function

will look for the substring starting at position 1.


 If n is not mention the function will look for the

first occurrence of the substring in the string


INSTR examples
SELECT INSTR('xyzabcxabcxabcxyz','abc') from dual
 4

SELECT INSTR('xyzabcxabcxabcxyz','abc',6) from dual


 8

SELECT INSTR('xyzabcxabcxabcxyz','abc',1,2) from dual


 8

SELECT INSTR('xyzabcxabcxabcxyz','abc',5,2) from dual


 12
SUBSTR, INSTR Together
SELECT Author,
SUBSTR(Author, INSTR(Author,',')+2 ) || ' ' ||
SUBSTR(Author,1, INSTR(Author,',')-1 )
FROM Magazine

Results:
Eminescu, Mihai Mihai Eminescu
Creanga, Ion Ion Creanga
Cosbuc, George George Cosbuc
SINGLE ROW
FUNCTIONS
1. NUMBERS MANIPULATION
ABS
 ABS(n)
 returns the absolute value of n
 ABS(10) = 10
 ABS(-5.8) = 5.8
CEIL, FLOOR
 Ceil(nr)
 FLOOR(nr)

CEIL returns the smallest integer that is greater


or equal to nr
FLOOR returns the largest integer that is smaller
or equal to nr
CEIL, FLOOR Example
 CEIL(nr)
 CEIL(2) = 2
 CEIL(1.3) = 2
 CEIL(1.8) = 2
 CEIL(-2.3) = -2
 FLOOR(nr)
 FLOOR(2) = 2
 FLOOR(1.3) = 1
 FLOOR(1.8) = 1
 FLOOR(-2.3) = -3
MOD
 MOD(n1,n2)

MOD divides n1 by n2 and tells the remainder


 MOD(100,10) = 0
 MOD(22,23) = 22
 MOD(-30.23,7) = -2.23
 7*(-4) + (–2.23) = - 30.23
 MOD(4.1,0.3) = 0.2
 0.3* 13 + 0.2 = 4.1
POWER
 POWER(n1,n2)

 POWER(3,2) = 9
 POWER(3,3) = 27
 POWER(3,1.086)=3.29726371
 POWER(64,0.5) = 8
ROUND
 ROUND(value,precision)
 ROUND(55.5) = 56
 ROUND(33.3) = 33
 ROUND(-55.5) = -56
 ROUND(-33.3) = -33
 ROUND(45.926,2) = 45.93
 ROUND(45.923,2) = 45.92
 ROUND(45.926,-1) = 50
 ROUND(42.926,-1) = 40
 ROUND(45.926,-2) = 0
 ROUND(65.926,-2) = 100
TRUNC
 TRUNC(value,precision)
 TRUNC(55.5) = 55
 TRUNC(33.3) = 33
 TRUNC(-55.5) = -55
 TRUNC(-33.3) = -33
 TRUNC(45.926,2) = 45.92
 TRUNC(45.923,2) = 45.92
 TRUNC(45.926,-1) = 50
 TRUNC(42.926,-1) = 40
 TRUNC(45.926,-2) = 0
 TRUNC(65.926,-2) = 0
Date Functions
 SYSDATE
 returneaza date&time a serverului;
 nu are parametri
 CURRENT_DATE
 returneaza data curenta de pe calculatorul local
 MONTH_BETWEEN(date1,date2)
 returneaza numarul de luni dintre doua date
calendaristice;
 valoarea returnata e un nr. real;
 Valoarea poate negativa (daca date2 e mai recenta
decat date1)
 ADD_MONTH(date,nr)
 Aduna un nr. de luni la o data calendaristica
SELECT sysdate, ADD_MONTH(sysdate,5)
FROM DUAL
=> 03-MAR-06 03-AUG-06
 NEXT_DAY(date,'Friday')
 returneaza urmatoarea zi de vineri de dupa ziua date (adica chiar daca
azi e fineri next_day(sysdate,'Friday') nu va returna ziua curenta ci
vinerea viitoare).
SELECT sysdate, NEXT_DAY(sysdate,'Monday'),
NEXT_DAY(sysdate,'Friday')
FROM dual
=> 03-MAR-06 06-MAR-06 10-MAR-06
 LAST_DAY(date)
 Returneaza ultima zi din luna din care face parte date
SELECT sysdate, LAST_DAY(sysdate),
LAST_DAY(sysdate+30)
FROM dual
=> 03-MAR-06 31-MAR-06 30-APR-06
 Observatie
 In Oracle pentru valorile de tip data calendaristica se memoreaza pe
langa data propriu-zisa si ore, minute, secunde.
SELECT '03-MAR-06'-sysdate FROM DUAL
=> 0.516 (sau ceva de genul acesta…)

 Pt. o data introdusa ca mai sus '03-MAR-06' (adica nu se specifica


ora), se presupune ora 12.00 AM.
 SYSDATE – include intotdeauna data si ora surenta
 ROUND(date)
 Rotunjeste date la ora 12AM (miezul noptii, inceputul zilei),
daca ora din date este inainte de amiaza (ora 12PM), respectiv la
ziua urmatoare (12AM, miezul noptii) daca ora e trecuta de
miezul zilei

 TRUNC(date)
 Trunchiaza date la ora 12 AM (miezul noptii, inceputul zilei
 ROUND(date,'Month')
 TRUNC(date,'Month')
 Rotunjeste/trunchiaza la inceput de luna, rotunjirea se face dupa 16 ale lunii,
inclusiv, la luna urmatoare

SELECT sysdate, ROUND(sysdate,'Month'),


TRUNC(sysdate,'Month'),
ROUND(sysdate+20,'Month'),
TRUNC(sysdate+20,'Month')
FROM dual

=> 03-MAR-06
01-MAR-06
01-MAR-06
01-APR-06
01-MAR-06
 ROUND(date,'Year')
 TRUNC(date,'Year')
 Rotunjeste/trunchiaza la inceput de an (rotunjirea se face dupa 1 iulie)

SELECT sysdate, ROUND(sysdate,'Year'),


TRUNC(sysdate,'Year'),
ROUND(ADD_MONTH(sysdate,4),'Year'),
TRUNC(ADD_MONTH(sysdate,4),'Year')
FROM dual

=> 03-MAR-06
01-JAN-06
01-JAN-06
01-JAN-07
01-MAR-06
Conversion Functions
Conversion Functions
Date -> Char
TO_CHAR (data, 'format')
 Cuvintele dintre ghilimele in format sunt inserate in sirul afisat
exact cum apar
 Spatiile ce apar informat sunt reproduse in rezultatul afisat
 fm – elimina spatiile sau zerourile nesemnificative

SELECT to_char(sysdate,'dd "din luna" mm "a anului" YYYY')


=> 10 din luna 03 a anului 2006

SELECT to_char(sysdate,'fmdd "din luna" mm "a anului" YYYY')


=> 10 din luna 3 a anului 2006
Conversion Functions
Date -> Char
YYYY
Select to_char(sysdate,'dd-mon-YyYy') from dual
=> 10-mar-2006
YY
Select to_char(sysdate,'dd-mon-YY') from dual
=> 10-mar-06
Y
Select to_char(sysdate,'dd-mon-Y') from dual
=> 10-mar-6
YEAR
Select to_char(sysdate,'dd-mon-Year') from dual
=> 10-mar-Two Thousand Six
Select to_char(sysdate,'dd-mon-YEAR') from dual
=> 10-mar-TWO THOUSAND SIX
Conversion Functions
Date -> Char
MM
Select to_char(sysdate,'dd-mm-YYYY') from dual
=> 10-03-2006
Select to_char(sysdate,'fmdd-mm-YYYY') from dual
=> 10-3-2006
MON
Select to_char(sysdate,'dd-mon-YYYY') from dual
=> 10-mar-2006
Select to_char(sysdate,'dd-MON-YYYY') from dual
=> 10-MAR-2006
Select to_char(sysdate,'dd-Mon-YYYY') from dual
=> 10-Mar-2006
Conversion Functions
Date -> Char
MONTH

Select to_char(sysdate,'dd-month-YYYY') from dual


=> 10-march-2006

Select to_char(sysdate,'dd-MONTH-YYYY') from dual


=> 10-MARCH-2006

Select to_char(sysdate,'dd-Month-YYYY') from dual


=> 10-March-2006
Conversion Functions
Date -> Char
SP – se poate combina cu YYY, DD, MM, HH, MI

Select to_char(sysdate,'dd-mm-YYYY') from dual


=> 10-03-2006

Select to_char(sysdate,'ddsp-mm-YYYY') from dual


=> ten-03-2006

Select to_char(sysdate,'ddspth-mm-YYYY') from dual


=> tenth-03-2006

Select to_char(sysdate-7,'ddspth-mm-YYYY') from dual


=> third-03-2006
Conversion Functions
Date -> Char
DD - ziua din luna (1..31)
Select to_char(sysdate,'dd-mm-YYYY') from dual
=> 10-03-2006

DDth DDTH (the spell is set by DD not the TH)


Select to_char(sysdate,'DDth-mm-YYYY') from dual
=> 10TH-03-2006

Ddth DdTH
Select to_char(sysdate,'DdTH-mm-YYYY') from dual
=> 10Th-03-2006

ddth ddTH
Select to_char(sysdate,'ddTH-mm-YYYY') from dual
=> 10th-03-2006
Conversion Functions
Date -> Char
D – ziua din saptamana (1..7)
Select to_char(sysdate,'d-mm-YYYY') from dual
=> 6-03-2006

DAY
Select to_char(sysdate,'Day, dd-mm-YYYY') from dual
=> Friday, 10-03-2006

DY – abrevierea cu trei caractere


Select to_char(sysdate,'DY, dd-mm-YYYY') from dual
=> FRI, 10-03-2006

DDD – ziua din an (1..366)


Select to_char(sysdate,'ddd "of" YYYY') from dual
=> 69 of 2006
Conversion Functions
Char -> Date
TO_DATE('data', 'format')

 Se folosesc aceleasi formate ca si la TO_CHAR cu urmatoarele restrictii:


 Nu se pot folosi siruri incluse intre ghilimele:
SELECT to_date('10 of March 2006','dd "of" March, YYYY')
=> eroare!
 Zilele nu pot fi scrise cu litere, trebuie sa fie numere
SELECT to_date('Ten March 2006','Ddsp March, YYYY')
=> eroare!
RR and YY format
RR and YY format
Select TO_CHAR(TO_DATE('01-01-97','dd-mm-yy'),'YYYY')
=>

Select TO_CHAR(TO_DATE('01-01-97','dd-mm-rr'),'RRR')
=>

Select TO_CHAR(TO_DATE('01-01-97','dd-mm-rr'),'YYYY')
=>

Select TO_CHAR(TO_DATE('01-01-97','dd-mm-yy'),'RRR')
=>

You might also like