Sie sind auf Seite 1von 22

WEEK-3A

1-The Built-In Character Functions


Name Description
ASCII Returns the ASCII code of a character.
CHR Returns the character associated with the specified collating code.
CONCAT Concatenates two strings into one.
INITCAP Sets the first letter of each word to uppercase. All other letters are set to
lowercase.
INSTR Returns the location in a string of the specified substring.
LENGTH Returns the length of a string.
LOWER Converts all letters to lowercase.
LPAD Pads a string on the left with the specified characters.
LTRIM Trims the left side of a string of all specified characters.
REPLACE Replaces a character sequence in a string with a different set of characters.
RPAD Pads a string on the right with the specified characters.
RTRIM Trims the right side of a string of all specified characters.
SUBSTR Returns the specified portion of a string.
TRANSLATE Translates single characters in a string to different characters.
UPPER Converts all letters in the string to uppercase.
The ASCII function

Returns the character associated with the specified collating code.

FUNCTION ASCII (single_character IN VARCHAR2) RETURN NUMBER

The ASCII function returns the NUMBER code that represents the
specified character in the database character set. The specification of
the ASCII function is:

Examples:

ASCII ('defg') ==> 100


ASCII ('d') ==> 100
ASCII ('d_e_f_g') ==> 100
CHR
Returns the character associated with the specified collating code.

FUNCTION CHR (code_location IN NUMBER) RETURN VARCHAR2

The CHR function is the inverse of ASCII. It returns a VARCHAR2 character


that corresponds to the location in the collating sequence provided as a
parameter. The specification of the CHR function is:

Examples: CHR(65)  A CHR(66)  B

TABLE : NUM SELECT CHR(NUMBER) FROM NUM;

NUMBER
65
A
66
B
67
C
CONCAT
Concatenates two strings into one.

The CONCAT function concatenates by taking two VARCHAR2 strings and


returning those strings appended together in the order specified. The
specification of the CONCAT function is:
FUNCTION CONCAT (string1 IN VARCHAR2, string2 IN VARCHAR2) RETURN VARCHAR2

Examples: CONCAT ('abc', 'defg') ==> 'abcdefg'


CONCAT (NULL, 'def') ==> 'def'

TABLE: SELECT CONCAT(NAME,SURNAME)


PERSONAL FROM PERSONAL

NAME SURNAME
ASET ISKAKOV ASETISKAKOV
ASEL OMAROVA
ASELOMAROVA
INITCAP
Sets the first letter of each word to uppercase. All other letters are set to
lowercase.

The INITCAP function reformats the case of the string argument, setting the
first letter of each word to uppercase and the remainder of the letters in that
word to lowercase. A word is a set of characters separated by a space or
nonalphanumeric characters (such as # or _ ). The specification of INITCAP is:

FUNCTION CONCAT (string1 IN VARCHAR2, string2 IN VARCHAR2) RETURN VARCHAR2

Examples: INITCAP ('this is lower') ==> 'This Is Lower'

SELECT INITCAP(NAME) FROM PERSONAL


TABLE: PERSONAL
NAME SURNAME
aset iskakov
Aset
asel omarova
Asel
LENGTH
Returns the length of a string.

The LENGTH function returns the length of the specified string. The
specification for LENGTH follows:

FUNCTION LENGTH (string1 VARCHAR2) RETURN NUMBER

LENGTH ('abc')  3 LENGTH (‘ABCDEF')  6

Examples:
TABLE: PERSONAL

NAME SURNAME
ASET ISKAKOV

SELECT LENGTH(SURNAME) FROM PERSONAL


7
LOWER
Converts all letters to lowercase.

The LOWER function converts all letters in the specified string to lowercase.
The specifications for the LOWER function are:

FUNCTION LOWER (string1 IN CHAR) RETURN CHAR


FUNCTION LOWER (string1 IN VARCHAR2) RETURN VARCHAR2

LOWER (‘ABC')  abc LENGTH (‘AbCd')  abcd

Examples:
TABLE: PERSONAL

NAME SURNAME SELECT LOWER(SURNAME) FROM PERSONAL


ASET ISKAKOV

iskakov
UPPER
Converts all letters to uppercase.

The UPPER function converts all letters in the specified string to uppercase.
The specifications for the UPPER function are:

FUNCTION UPPER (string1 IN CHAR) RETURN CHAR


FUNCTION UPPER (string1 IN VARCHAR2) RETURN VARCHAR2

UPPER (‘abc')  ABC UPPER(‘AbCd')  ABCD

Examples:
TABLE: PERSONAL

NAME SURNAME SELECT UPPER(SURNAME) FROM PERSONAL


Aset Iskakov

ISKAKOV
TRANSLATE
Replace characters

Examples:

TRANSLATE (‘HOW ARE YOU’,’AY’,’EM’  HOW ERE MOU

TABLE: P

NAME SURNAME
ASET ISKAKOV
TALGAT MIRZAYEV

SELECT TRANSLATE(NAME,’AT’,’UN’) FROM P; USEN


NULGUN
SUBSTR
Examples:

SUBSTR (‘HOW ARE YOU’,1,3)  HOW

SUBSTR (‘HOW ARE YOU’,5,3)  ARE

TABLE: P

NAME SURNAME
ASET ISKAKOV
TALGAT MIRZAYEV

SELECT SUBSTR(NAME,2,3) FROM P; SET


ALG
INSTR
Returns the location in a string of the specified substring.

The INSTR function searches a string to find a match for the substring and, if
found, returns the position, in the source string, of the first character of that
substring. If there is no match, then INSTR returns 0. In Oracle7, if
nth_appearance is not positive (i.e., if it is 0 or negative), then INSTR always
returns 1. In Oracle8, a value of 0 or a negative number for nth_appearance
causes INSTR to raise the VALUE_ERROR exception.

FUNCTION INSTR (string1 IN VARCHAR2, string2 IN VARCHAR2


[,start_position IN NUMBER := 1
[, nth_appearance IN NUMBER := 1]])
RETURN NUMBER
Examples:
TABLE: PERSONAL
NAME SURNAME
abc abcanaa

Find from Left

SELECT INSTR(SURNAME,’a’,1,3)
FROM PERSONAL

1 2 3 4 5 6 7

abcanaa 6
1 2 3
Examples:
TABLE: PERSONAL
NAME SURNAME
abc abcanaa

Find from Right

SELECT INSTR(SURNAME,’a’,-1,3)
FROM PERSONAL

1 2 3 4 5 6 7

4 abcanaa
3 2 1
RTRIM
Remove characters from right
Examples:

RTRIM (‘HOW ARE YOU ‘)  ‘HOW ARE YOU’

RTRIM(‘HOW ARE YOU’, ’YOU’)  ‘HOW ARE’

TABLE: P

NAME SURNAME
ASET ISKAKOV
TALGAT MIRZAYEV

SELECT RTRIM(NAME,’T’) FROM P; ASE


TALGA
LTRIM
Remove characters from left
Examples:

LTRIM(‘ HOW ARE YOU‘)  ‘HOW ARE YOU’

LTRIM (‘HOW ARE YOU’, ’HOW’)  ‘ARE YOU’

TABLE: P

NAME SURNAME
ASET ISKAKOV
TALGAT MIRZAYEV

SELECT LTRIM(NAME,’TA’) FROM P; SET


LGAT
LPAD
The LPAD function adds characters to the head of a character string

Examples:

LPAD(‘SDU’,7,’A’) AAAASDU

LPAD(‘SDU’,7,’ABCDEFG’) ABCDSDU

TABLE: P

NAME SURNAME
ASET ISKAKOV
TALGAT MIRZAYEV
OKOKOKASET
SELECT LPAD(NAME,10,’OK’) FROM P;
OKOKTALGAT
RPAD
The RPAD function adds characters to the end of a character string

Examples:

RPAD(‘SDU’,7,’A’) SDUAAAA

RPAD(‘SDU’,7,’ABCDEFG’) SDUABCD

TABLE: P

NAME SURNAME
ASET ISKAKOV
TALGAT MIRZAYEV
ASETOKOKOK
SELECT RPAD(NAME,10,’OK’) FROM P;
TALGATOKOK
REPLACE
The REPLACE function returns a string in which all occurrences of a specified
match string are replaced with a replacement string
Examples:

REPLACE(‘CAT CALL ’,’C’) AT ALL

REPLACE(‘HE IS 50 YEARS OLD’,’50’,’20’)  HE IS 20 YEARS OLD

TABLE: P

NAME SURNAME
ASET ISKAKOV
TALGAT MARATOV
ISKAKOV
SELECT REPLACE(SURNAME,’TOV’,’SIN’)
FROM P; MARASIN
TASKS
TABLE : CLASS
1-Write an SQL statement to returns name and surname separately.
STUDENT CLASS
NAME SURNAME
MULDASHEV.NURLAN 3A
-----------------------------------------------
NURUMBETOV.SYRYM 4A NURLAN MULDASHEV
SYRYM NURUMBETOV

2-Write an SQL statement to update the table as shown figure below.

STUDENT CLASS
Nurlan MULDASHEV 3A
Syrym NURUMBETOV 4A

3-Write an SQL statement to returns the index of the books as shown in the figure below.

TABLE : BOOKS
BOOKNAME TOTALPAGE BOOKNAME TOTALPAGE
-----------------------------------------------------------
MAGIC BOOK 520
MAGIC BOOK……………………… 520
X-MAN 400 X-MAN……………………………… 400

4-Write two SQL statement to returns the position of the third ‘or’ in the following string. (search from left and right)

Corporate Floor
5-Write an SQL statement to returns the position of the first “FOUR” from the string: Take the first four characters
6-Write an SQL statement to returns the position of the second “ten” from the string : seven ten eight nine ten one
7-Write an SQL statement to returns the position of the second “a” from the string : Take the first four characters
8-Write an SQL statement to returns the value between two commas from the STRING table below.

TABLE : string
text
---------------
Abc,def,gfh
Sd,fghjk,gh
Asdfgh,f,fghjjk

9-Write an SQL statement to returns JOBs in ascending order by its character number.
10-Write an SQL statement to returns names without any spaces from both sides.
TABLE : students
Name
----------
Nurlan
Asem
Aynura

Write an SQL statement to update ENAME column from table “EMP“ as shown below.
11-Change ‘A’ with ‘X’
12-Change ‘N’ with ‘S’ and ‘M’ with ‘9’
13-Write an SQL statement to returns the ASCII code of the third character from “ename” in EMP.
1) select substr(STUDENT,1,instr(STUDENT,'.')-1) AS NAME, substr(STUDENT,instr(STUDENT,'.')+1),LENGTH(STUDENT)) AS
SURNAME from class;
2) UPDATE CLASS SET STUDENT=SUBSTR(STUDENT,INSTR(STUDENT,'.')+1,LENGTH(STUDENT))||' '||
SUBSTR(STUDENT,1,INSTR(STUDENT,'.')-1) WHERE STUDENT IS NOT NULL;
3) select rpad(bookname,20,'.'),totalpage from books;
4)
a - select instr('Corporate floor','or',1,3) from class;
b - select instr('Corporate floor','or',-1,3) from class;

5) select instr('Take the first four characters','four',1,1) from class;


6) select instr('seven ten eight nine ten one','ten',1,2) from class;
7) select instr('Take the first four characters','a',1,2) from class;
8) select substr(text,instr(text,',',1,1)+1,instr(text,',',1,2)-instr(text,',',1,1)-1)) from string;
9) select job from emp order by length(job);
10)select trim(Name) from students;
11)select translate(ename,'A','X') from EMP;
12)select translate(ename,'AS','X9') from EMP;
13)select ascii(substr(ename,3,1)) from emp;

Das könnte Ihnen auch gefallen