SQL Strings Notes 1
SQL Strings Notes 1
SQL Strings Notes 1
Please include the below MySQL functions in syllabus with example and explanations when you
will be delivering the MySQL topics –
2) INSTR Returns the position of the first occurrence of a string in another string
The INSTR() function returns the position of the first occurrence of a string in another string.
Example
Parameter Values
Parameter Description
Return Values
Syntax
Parameter Values
Parameter Description
Example
Example
The NULLIF function in SQL Server accepts two arguments and compares them to determine they
are the same or different. If both arguments are equal, it will return a NULL value. If the arguments
are not identical, they will return the first argument.
The NULLIF() function returns NULL if two expressions are equal, otherwise it returns the first
expression.
Syntax
NULLIF(expr1, expr2)
Parameter Values
Parameter Description
Example
Example
Example
SELECT CURDATE();
Syntax
CURDATE()
Example
Return the current date + 1:
SELECT CURDATE() + 1;
7) USER Returns the current MySQL user name and host name
String functions
are used to perform an operation on input string and return an output string.
Following are the string functions defined in SQL:
1. ASCII(): This function is used to find the ASCII value of a character.
Syntax: SELECT ascii('t');
Output: 116
2. CHAR_LENGTH(): Doesn’t work for SQL Server. Use LEN() for SQL
Server. This function is used to find the length of a word.
Syntax: SELECT char_length('Hello!');
Output: 6
LCASE(): This function is used to convert the given string into lower
case.
Syntax: LCASE ("GeeksFor Geeks To Learn");
Output: geeksforgeeks to learn
LEFT(): This function is used to SELECT a sub string from the left of
given size or characters.
Syntax: SELECT LEFT('geeksforgeeks.org', 5);
Output: geeks
LOCATE(): This function is used to find the nth position of the given
word in a string.
Syntax: SELECT LOCATE('for', 'geeksforgeeks', 1);
Output: 6
LOWER(): This function is used to convert the upper case string into
lower case.
Syntax: SELECT LOWER('GEEKSFORGEEKS.ORG');
Output: geeksforgeeks.org
LPAD(): This function is used to make the given string of the given
size by adding the given symbol.
Syntax: LPAD('geeks', 8, '0');
Output:
000geeks
LTRIM(): This function is used to cut the given sub string from the
original string.
Syntax: LTRIM('123123geeks', '123');
Output: geeks
MID(): This function is to find a word from the given position and of
the given size.
Syntax: Mid ("geeksforgeeks", 6, 2);
Output: for
REPEAT(): This function is used to write the given string again and
again till the number of times mentioned.
Syntax: SELECT REPEAT('geeks', 2);
Output: geeksgeeks
RIGHT(): This function is used to SELECT a sub string from the right
end of the given size.
Syntax: SELECT RIGHT('geeksforgeeks.org', 4);
Output: ‘.org’
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('geeks', 8, '0');
Output: ‘geeks000’
RTRIM(): This function is used to cut the given sub string from the
original string.
Syntax: RTRIM('geeksxyxzyyy', 'xyz');
Output: ‘geeks’
SUBSTR(): This function is used to find a sub string from the a string
from the given position.
Syntax:SUBSTR('geeksforgeeks', 1, 5);
Output: ‘geeks’
TRIM(): This function is used to cut the given symbol from the string.
Syntax: TRIM(LEADING '0' FROM '000123');
Output: 123