CLASS XII MySQL String Functions

 In Class XII CBSE Computer Science (Code 083) and Informatics Practices (Code 065), String (Text) Functions are a vital part of the MySQL syllabus. These are Single Row Functions that operate on a single value and return a single value.




Core MySQL String Functions (CBSE Syllabus)

1. Case Conversion Functions

These functions change the letter case of the input string.

FunctionPurposeExampleResult
LOWER() / LCASE()Converts characters to lowercase.SELECT LOWER('CBSE');'cbse'
UPPER() / UCASE()Converts characters to uppercase.SELECT UPPER('class 12');'CLASS 12'

2. Length & Position Functions

Used to measure strings or find the location of specific characters.

FunctionPurposeExampleResult
LENGTH()Returns the number of characters in a string.SELECT LENGTH('Informatics');11
INSTR()Returns the position of the first occurrence of a substring.SELECT INSTR('Computer', 'mp');3

3. Substring Extraction Functions

Used to pull out a specific portion of a string. Note: In MySQL, the index starts at 1.

FunctionPurposeExampleResult
LEFT()Returns the leftmost 'n' characters.SELECT LEFT('Science', 3);'Sci'
RIGHT()Returns the rightmost 'n' characters.SELECT RIGHT('Science', 3);'nce'
SUBSTR() / MID()Extracts a string from a start position for 'n' characters.SELECT SUBSTR('Database', 3, 4);'taba'

4. Trimming Functions

Used to remove unwanted leading or trailing spaces.

  • LTRIM(str): Removes leading spaces (left side).

    • Example: SELECT LTRIM(' Hello'); $\rightarrow$ 'Hello'

  • RTRIM(str): Removes trailing spaces (right side).

    • Example: SELECT RTRIM('Hello '); $\rightarrow$ 'Hello'

  • TRIM(str): Removes spaces from both sides.

    • Example: SELECT TRIM(' Hello '); $\rightarrow$ 'Hello'


Important Board Exam Concepts

A. Nested String Functions

The board often asks for the output of functions combined together. Solve them from the inside out.

Example:

SELECT UPPER(SUBSTR('informaticspractices', 1, 11));

  1. SUBSTR(..., 1, 11) $\rightarrow$ 'informatics'

  2. UPPER('informatics') $\rightarrow$ 'INFORMATICS'

B. String Indexing

Unlike Python (where index starts at 0), MySQL indexing starts at 1.

  • SELECT SUBSTR('HELLO', 1, 1); $\rightarrow$ 'H' (Not 'E')


Solved Board-Style Questions

Q1. Write the output of:

SELECT RIGHT(LTRIM(' PYTHON'), 3);

  • Step 1: LTRIM removes spaces $\rightarrow$ 'PYTHON'

  • Step 2: RIGHT(..., 3) $\rightarrow$ 'HON'

Q2. Write a SQL command to display the first 3 characters of the column StudentName.

  • Ans: SELECT LEFT(StudentName, 3) FROM Students;

Q3. What is the difference between LENGTH() and INSTR()?

  • Ans: LENGTH() returns the total number of characters in a string, whereas INSTR() returns the numerical position of a specific substring within a string.




Post a Comment

Please do note create link post in comment section

Previous Post Next Post