Character / String Functions
Character / String Functions
Conversion Functions:
Bin_To_Num NumToDSInterval To_Date To_Number
Cast NumToYMInterval To_DSInterval To_Single_Byte
CharToRowid RawToHex To_Lob To_Timestamp
From_Tz To_Char To_Multi_Byte To_Timestamp_Tz
HexToRaw To_Clob To_NClob To_YMInterval
Advanced Functions:
BFilename Group_ID NULLIF User
Cardinality Lag NVL UserEnv
Case Statement Lead NVL2
Coalesce LNNVL Sys_Context
Decode NANVL Uid
Mathematical Functions:
Abs Covar_pop Max Sqrt
Acos Covar_samp Median StdDev
Asin Count Min Sum
Atan Cume_Dist Mod Tan
Atan2 Dense_Rank Power Tanh
Avg Exp Rank Trunc (numbers)
Bin_To_Num Extract Remainder Trunc (dates)
BitAnd Floor Round (numbers) Var_pop
Ceil Greatest Round (dates) Var_samp
Corr Least Sign Variance
Cos Ln Sin
Cosh Log Sinh
Date Functions:
Add_Months Last_Day Round To_Date
Current_Date LocalTimestamp SessionTimeZone Trunc
Current_Timestamp Months_Between Sysdate Tz_Offset
DbTimeZone New_Time SysTimestamp
From_Tz Next_Day To_Char
Error Functions:
SQLCODE
SQLERRM
Oracle pl/sql trim function
The trim function removed characters from beginning and/or end of a string in Oracle. Oracle has 3
functions for this:
TRIM
The TRIM function trims specified characters from the left and/or right.
If no characters are specified, the left and right spaces are left out.
Example: trim(' Oradev dot com ') = 'Oradev dot com'.
Another option is:
trim(trailing 'a' from 'aaaabbaaaa') which results in 'aaaabb' or
trim(leading 'a' from 'aaaabbaaaa') which results in 'bbaaaa' or
trim(both 'a' from 'aaaabbaaaa') which results in 'bb'.
LTRIM
LTRIM removes characters from the left of a string if they are equal to the specified string. Example:
ltrim('aaaaaabc','a') = 'bc' If the last parameter is not specified, spaces are removed from the left side.
RTRIM
RTRIM removes characters from the right of a string if they are equal to the specified string. Example:
rtrim('bcaaaaaa','a') = 'bc' If the last parameter is not specified, spaces are removed from the right side.
The ASCII function returns the decimal representation in the database character
ASCII
set of the first character of char. Example: ASCII('b') =98
The CHR function returns the character having the binary equivalent to n as a
CHR VARCHAR2 value in either the database character set. Example: CHR(10)||
CHR(13) = carriage return plus line feed.
The COALESCE function returns the first non-null expr in the expression list. At
least one expr must not be the literal NULL. If all occurrences of expr evaluate to
COALESCE
null, then the function returns null. Example: select COALESCE(col1, col2, col3)
FROM emp;
CONCAT The CONCAT function returns the concatenation of 2 strings. You can also use
the || command for this. Example: CONCAT('abc','def') = 'abcdef'
The CONVERT function converts a string from one characterset to another. The
datatype of the returned value is VARCHAR2. Example: CONVERT('This is an
CONVERT
example','UTF-8','WE8ISO8859P1') SELECT CONVERT('Ä Ê Í Ó Ø A B C D E
', 'US7ASCII', 'WE8ISO8859P1') FROM DUAL; = A E I ? ? A B C D E ?
The DUMP function returns a VARCHAR2 value containing the datatype code,
DUMP length in bytes, and internal representation of expr. The returned result is always
in the database character set.
Returns the position of a String within a String. For more information see Oracle
INSTR
instr function
INITCAP Transform String to init cap Example: INITCAP('ORADEV') = 'Oradev'
INSTRB Returns the position of a String within a String, expressed in bytes.
Returns the position of a String within a String, expressed in Unicode complete
INSTRC
characters
INSTR2 Returns the position of a String within a String, expressed in UCS2 code points
INSTR4 Returns the position of a String within a String, expressed in UCS4 code points
The LENGTH functions returns the length of char. LENGTH calculates length
LENGTH using characters as defined by the input character set. Example:
length('oradev.com') = 10
LENGTHB Returns the length of a string, expressed in bytes.
The LOWER function returns a string with all lower case characters. Example:
LOWER
LOWER('ORADEV') = 'oradev'
Add characters to the left of a string until a fixed number is reached. Example:
LPAD lpad('abc',8,'x') = 'xxxxxabc'. If the last parameter is not specified, spaces are
added to the left.
LTRIM removed characters from the left of a string if they are equal to the
LTRIM specified string. Example: ltrim('aaaaaabc','a') = 'bc' If the last parameter is not
specified, spaces are removed from the left side.
The replace function replaces every occurrence of a search_string with a new
REPLACE string. If no new string is specified, all occurrences of the search_string are
removed. Example: replace('a1a1a1','a','2') = '212121'.
Reverses the characters of a String. Example: REVERSE('oradev.com') =
REVERSE
'moc.vedaro'
Add characters to the right of a string until a fixed number is reached. Example:
RPAD rpad('abc',8,'x') = 'abcxxxxx'. If the last parameter is not specified, spaces are
added to the right.
RTRIM removed characters from the right of a string if they are equal to the
RTRIM specified string. Example: rtrim('bcaaaaaa','a') = 'bc' If the last parameter is not
specified, spaces are removed from the right side.
SOUNDEX returns a character string containing the phonetic representation of
char. This function lets you compare words that are spelled differently, but sound
SOUNDEX
alike in English. Example: select * from emp where lastname
SOUNDEX('SMITH');
SUBSTR Returns a substring. For more information see Oracle substring
SUBSTRB Returns a substring expressed in bytes instead of characters.
SUBSTRC Returns a substring expressed in Unicode code points instead of characters.
SUBSTR2 Returns a substring using USC2 code points.
SUBSTR4 Returns a substring using USC4 code points.
TRANSLATE returns expr with all occurrences of each character in from_string
replaced by its corresponding character in to_string. Characters in expr that are
TRANSLATE
not in from_string are not replaced. Example: SELECT TRANSLATE('SQL*Plus
User''s Guide', ' */''', '___') FROM DUAL; = 'SQL_Plus_Users_Guide'
The TRIM function trims specified characters from the left and/or right. If no
TRIM characters are specified, the left and right spaces are left out. Example: trim('
Oradev dot com ') = 'Oradev dot com'.
|| (pipes) With pipes you can concattenate strings. Example 'Oradev'||'.com' = 'Oradev.com'.
Transform a string to all upper case characters. Example: UPPER('oradev') =
UPPER
'ORADEV'
VSIZE The VSIZE function returns the byte size of a String.
Examples
INSTR('CORPORATE FLOOR','OR', 3, 2) = 14
INSTR('CORPORATE FLOOR','OR', -3, 2) = 2
INSTR('ab ab ab','ab') = 1
INSTR('ab ab ab','ab',1,2) = 4
INSTR('ab ab ab','ab',2,2) = 7
INSTR('abcabcabcdef','de') = 7
All number format models cause the number to be rounded to the specified number of significant digits.
If a value has more significant digits to the left of the decimal place than are specified in the format,
then pound signs (#) replace the value. If a positive value is extremely large and cannot be represented
in the specified format, then the infinity sign (~) replaces the value. Likewise, if a negative value is
extremely small and cannot be represented by the specified format, then the negative infinity sign
replaces the value (-~).
Element Example Description
Returns a comma in the specified position. You can specify multiple
commas in a number format model.
Restrictions:
, 9,999
(comma)
A comma element cannot begin a number format model.
Restriction: The MI format element can appear only in the last position of a
number format model.
Returns negative value in <angle brackets>.
Restriction: The PR format element can appear only in the last position of a
number format model.
Returns a value as Roman numerals in uppercase.
RN RN
Returns a value as Roman numerals in lowercase.
rn rn
Value can be an integer between 1 and 3999.
Element Example Description
Returns negative value with a leading minus sign (-).
Restriction: The S format element can appear only in the first or last
position of a number format model.
"Text minimum". Returns (in decimal output) the smallest number of
characters possible. This element is case-insensitive.
The default is TM9, which returns the number in fixed notation unless the
output exceeds 64 characters. If output exceeds 64 characters, then Oracle
automatically returns the number in scientific notation.
TM TM
Restrictions:
You can follow this element only with 9 or E (only one) or e (only
one).
Returns in the specified position the "Euro" (or other) dual currency symbol
U U9999
(the current value of the NLS_DUAL_CURRENCY parameter).
Returns a value multiplied by 10n (and if necessary, round it up), where n is
V 999V99
the number of 9's after the "V".
Returns the hexadecimal value of the specified number of digits. If the
specified number is not an integer, then Oracle rounds it to an integer.
Restrictions:
XXXX
X This element accepts only positive values or 0. Negative values
xxxx return an error.
You can precede this element only with 0 (which returns leading
zeroes) or FM. Any other elements return an error. If you specify
neither 0 nor FM with X, then the return always has 1 leading blank.
Oracle to_date function
The oracle to_date function converts a string in a specified format to an Oracle date format.
Syntax
to_date('formatted string'); (returns a date using the default oracle date format)
to_date('formatted string','format string'); (returns a date using the format string specified)
to_date('formatted string','format string','nls description');(returns a date using the format string
specified and using the specified NLS settings)
Example:
to_date('01-JAN-2006');
to_date('01-01-2004','DD-MM-YYYY');
to_date('31-12-2006 23:34:59','DD-MM-YYYY HH24:MI:SS');
to_date('01-JAN-99''DD-MON-YY,'nls_date_language = American');
syntax
substr([input],[start],[length]) or
substr([input],[start]) or
With input the String to take a substring from,
start is the starting position where 1 is the first character. (if you pass 0, this will be substituted by 1) and
the optional length parameter is the number of characters in the substring. If length is left out, then
substr will return the substring from position start till the end of the input-string.
Sample code:
select substr('1234567890',3,2) from dual;
will return: '34'.
select substr('1234567890',7) from dual;
will return: '789'