Friday, December 20, 2024

MySQL ASCII() Function

 The ASCII() function in MySQL returns the numeric value of the leftmost character in a given string. This numeric value corresponds to the ASCII code of the character.

Syntax:

ASCII(string)

Parameters:

  • string: The input string whose leftmost character's ASCII value is to be returned.

Returns:

  • The ASCII code of the first character in the string.
  • If the string is empty, it returns 0.
  • If the input is NULL, it returns NULL.

Examples:

  1. Basic Example:

    SELECT ASCII('A');
    

    Output: 65 (ASCII code of A).

  2. Multiple Characters:

    SELECT ASCII('Hello');
    

    Output: 72 (ASCII code of H, the leftmost character).

  3. Empty String:

    SELECT ASCII('');
    

    Output: 0

  4. Numeric Input as String:

    SELECT ASCII('123');
    

    Output: 49 (ASCII code of 1).

  5. Special Characters:

    SELECT ASCII('!');
    

    Output: 33 (ASCII code of !).

  6. NULL Input:

    SELECT ASCII(NULL);
    

    Output: NULL

Use Cases:

  • Determining ASCII values for data manipulation or transformation.
  • Encoding or decoding purposes.
  • String comparison or analysis at the character level.

Let me know if you need more detailed examples or explanations!

No comments:

Post a Comment