String Functions
String Functions
• String functions
are used to perform an operation on input
string and return an output string.
Following are the string functions defined in
SQL:
• ASCII(): This function is used to find the
ASCII value of a character.
• Syntax: SELECT ASCII('t') FROM DUAL;
• Output: 116
• INSTR(): This function is used to find the
occurrence of an alphabet.
• Syntax: INSTR(‘HELLO HELLO', ‘E');
• Output: 2 (the first occurrence of ‘E’)
• LOWER(): This function is used to convert
the upper case string into lower case.
• Syntax: SELECT LOWER(‘HELLO');
• Output: hello
• LPAD(): This function is used to make the
given string of the given size by adding the
given symbol.
• Syntax: LPAD(‘hello', 8, '0');
• Output: 000hello
• LTRIM(): This function is used to cut
the given sub string from the original
string.
• Syntax: LTRIM('123123hello', '123');
• Output: hello
• REPLACE(): This function is used to
cut the given string by removing the
given sub string.
• Syntax: REPLACE('123hello123', '123');
• Output: hello
• RPAD(): This function is used to make
the given string as long as the given size
by adding the given symbol on the right.
• Syntax: RPAD(‘hello', 8, '0');
• Output: ‘hello000’
• RTRIM(): This function is used to cut
the given sub string from the original
string.
• Syntax: RTRIM(‘helloxyxzyyy', zyy');
• Output: ‘helloxy’
• STRCMP(): This function is used to compare
2 strings.
– If string1 and string2 are the same, the STRCMP
function will return 0.
– If string1 is smaller than string2, the STRCMP
function will return -1.
– If string1 is larger than string2, the STRCMP
function will return 1.
• Syntax:
• SELECT STRCMP('google.com',
‘microsoft.com');
• Output: -1
• SUBSTR(): This function is used to find a sub
string from the a string from the given
position.
• Syntax: SUBSTR(‘hello’, 1, 3);
• Output: ‘hel’
• CONCAT() :To join two or more strings into
one, you use the CONCAT() function with the
following syntax:
• CONCAT ( input_string1, input_string2
[, input_stringN ] );
• The LEN() function returns the number
of characters of an input string, excluding
the trailing blanks.
• The following shows the syntax of
the LEN() function:
• LEN(input_string)
• SELECT LEN('SQL Server LEN') length,
LEN('SQL Server LEN ')
length_with_trailing_blanks;
Arithmetic function
Functions Description
ABS() This SQL ABS() returns the absolute value of a number passed as an
argument.
CEIL() This SQL CEIL() will rounded up any positive or negative decimal value
within the function upwards.
FLOOR() The SQL FLOOR() rounded up any positive or negative decimal value down
to the next least integer value.
EXP() The SQL EXP() returns e raised to the n-th power(n is the numeric
expression), where e is the base of natural algorithm and the value of e is
approximately 2.71828183.
MOD() This SQL MOD() function returns the remainder from a division.
POWER() This SQL POWER() function returns the value of a number raised to another,
where both of the numbers are passed as arguments.
SQRT() The SQL SQRT() returns the square root of given value in the argument.
• Select abs(-7.5) from dual
• Output: 7.5
• Select power(2,10) from dual
• Output: 1024
• Select mod(11,3) from dual
• Output: 2
• Select sqrt(64) from dual
• Output: 8
• SELECT CEIL(17.36) FROM dual;
• Output: 18
• SELECT FLOOR(17.36) FROM dual;
• Output : 17