Connecting Tech Pros Worldwide Forums | Help | Site Map

CHARACTER FUNCTIONS

amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#1   Sep 17 '07
CHARACTER Functions:

LOWER(string) - Returns string in lower case
Expand|Select|Wrap|Line Numbers
  1. SELECT LOWER('AAAA') FROM dual -- returns 'aaaa'
  2.  
UPPER(string) - Returns string in UPPER CASE
Expand|Select|Wrap|Line Numbers
  1. SELECT UPPER('aaaa') FROM dual -- returns 'AAAA'
  2.  
INITCAP(string) - Returns string with first character in UPPER CASE and other characters in lower case
Expand|Select|Wrap|Line Numbers
  1. SELECT INITCAP('aaaa') FROM dual -- returns 'Aaaa'
  2.  
SUBSTR(string,m,[n]) - Returns n no of characters from position m for string (n is optional)
Expand|Select|Wrap|Line Numbers
  1. SELECT SUBSTR('Welcome to India',1,3) from dual -- returns 'Wel'
  2.  
INSTR(string,c) - Returns the first position of character c from string. INSTR is case sensitive
Expand|Select|Wrap|Line Numbers
  1. SELECT INSTR('Well Done', 'D') FROM dual -- returns 6
  2. SELECT INSTR('Well Done', 'Well') FROM dual -- returns 1 (Here the string 'Well' matches with the part of string 'Well Done', so it will return the position of the first character ie 'W' here)
  3. SELECT INSTR('Well Done' ,'d') FROM dual -- returns 0
  4.  
LENGTH(string) - Returns length of string
Expand|Select|Wrap|Line Numbers
  1. SELECT LENGTH('aaaa') FROM dual -- returns 4
  2.  
RPAD(string,m,[c]) - Returns string of length m right padded with character c. If c is not specified, then the string will be right padded with SPACE
Expand|Select|Wrap|Line Numbers
  1. SELECT RPAD('aaaa',6,'*') FROM dual -- returns 'aaaa**'
  2. SELECT RPAD('aaaa',2,'*') FROM dual -- returns 'aa'
  3. SELECT RPAD('aaaa',12) FROM dual -- returns 'aaaa        '
  4.  
LPAD(string,m,[c]) - Returns string of length m left padded with character c. If c is not specified, then the string will be left padded with SPACE.
Expand|Select|Wrap|Line Numbers
  1. SELECT RPAD('aaaa',6,'*') FROM dual -- returns '**aaaa'
  2. SELECT RPAD('aaaa',2,'*') FROM dual -- returns 'aa'
  3. SELECT RPAD('aaaa',12) FROM dual -- returns '        aaaa'
  4.  
CONCAT(string1,string2) - Returns concatenated string string1string2
Expand|Select|Wrap|Line Numbers
  1. SELECT CONCAT('Well','Done') FROM dual -- returns 'WellDone'
  2.  
CHR(m) - Returns character value of number m
Expand|Select|Wrap|Line Numbers
  1. SELECT CHR(65) FROM dual -- returns 'A'
  2.  
ASCII(c) - Returns ASCII number of character c
[code=oracle]
SELECT ASCII('A') FROM dual -- returns 65
SELECT ASCII('ABCD') FROM dual -- returns 65 (ASCII value of first character only)

TRIM(string) - Returns string after trimming spaces on both the sides of the string
Expand|Select|Wrap|Line Numbers
  1. SELECT TRIM('    sfsdfsfs         ') FROM dual -- returns 'sfsdfsfs'
  2.  
LTRIM(string) - Returns string after trimming spaces on left side of the string
Expand|Select|Wrap|Line Numbers
  1. SELECT TRIM('    sfsdfsfs         ') FROM dual -- returns 'sfsdfsfs         '
  2.  
RTRIM(string) - Returns string after trimming spaces on right side of the string
Expand|Select|Wrap|Line Numbers
  1. SELECT TRIM('    sfsdfsfs         ') FROM dual -- returns '    sfsdfsfs'
  2.  
REPLACE(string,m,[c]) - Returns string after replacing all the occurances of character m with character c. If c is not specified, then the character m is truncated from the string
Expand|Select|Wrap|Line Numbers
  1. SELECT REPLACE('Welcomes','s','d') FROM dual -- returns 'Welcomed'
  2. SELECT REPLACE('ab','a','ab') FROM dual -- returns 'abb'
  3. SELECT REPLACE('ab','ac','ab') FROM dual -- returns 'ab' (Here no replacement takes place because there is no match between string and m. Here string = 'ab' and m= 'ac')
  4.  
TRANSLATE(string,m,c) - Returns string after replacing all the occurances of character m with character c. Here c is mandatory. If m is more than one character, then the occurance of any character of m in string will be replaced with character c
Expand|Select|Wrap|Line Numbers
  1. SELECT TRANSLATE('AA','Aa','bc') FROM dual -- returns 'bb' (Here 'A' is replaced by 'b' in string 'AA')
  2. SELECT TRANSLATE('Aa','Aa','bcfsfffssf') FROM dual -- returns 'bc' (Here 'Aa' is replaced with first two characters 'bc')
  3.  



Closed Thread