Tuesday, 10 July 2018

Mysql ASCII() function

Mysql ASCII() 

MySQL ASCII() returns the ASCII value of the leftmost character of a given string.
Syntax:
ASCII(str1)
Argument
NameDescription
str1A string whose ASCII value of the first character is to be retrieved.

The NULL within an ASCII function will return NULL.
The ASCII() will return 0 If the string is empty.
MySQL Version: 5.6

Example: MySQL ASCII() function
The following MySQL statement will return the ASCII value of b and B.
Code:
SELECT ASCII('b')AS Lower_Case, ASCII('B') AS Upper_Case;


Sample Output:
mysql> SELECT ASCII('b')AS Lower_Case, ASCII('B') AS Upper_Case; 
+------------+------------+
| Lower_Case | Upper_Case |
+------------+------------+
|         98 |         66 | 
+------------+------------+
1 row in set (0.03 sec)     
Example of MySQL ASCII() function using where clause
The following MySQL statement will return those authors (from author table), whose name's first character's ASCII value is less than seventy.
Code:
SELECT aut_name,ASCII(aut_name)as "ASCII value of 1st character" 
FROM author 
WHERE ASCII(aut_name)<70;


Sample table: author

Sample Output:
mysql> SELECT aut_name,ASCII(aut_name)as "ASCII value of 1st character" 
    -> FROM author 
    -> WHERE ASCII(aut_name)< 70; 
+--------------+------------------------------+
| aut_name     | ASCII value of 1st character |
+--------------+------------------------------+
| Evan Hayek   |                           69 | 
| E. Howard    |                           69 | 
| C. J. Wilde  |                           67 | 
| Butler Andre |                           66 | 
+--------------+------------------------------+
4 rows in set (0.10 sec)     
PHP script
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-ascii-function - php MySQL examples | w3resource</title>
<meta name="description" content="example-ascii-function - php MySQL examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>List of authors whose names first character's ASCII values is less than 70 :</h2>
<table class='table table-bordered'>
<tr>
<th>Names of Authors</th><th>ASCII value of 1st character</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("MySQL:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT aut_name,ASCII(aut_name)as "ASCII value of 1st character"
FROM author
WHERE ASCII(aut_name)<70') as $row) {
echo "<tr>";
echo "<td>" . $row['aut_name'] . "</td>";
echo "<td>" . $row['ASCII value of 1st character'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>



JSP script
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-ascii-function</title>
</head>
<body>
<%
try {
Class.forName("com.MySQL.jdbc.Driver").newInstance();
String Host = "jdbc:MySQL://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT aut_name,ASCII(aut_name)as 'ASCII value of 1st character' FROM author WHERE ASCII(aut_name)<70";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Names of Authors</td>
<td>ASCII value of 1st character</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("aut_name")%></TD>
<TD><%=rs.getString("ASCII value of 1st character")%></TD>
</TR>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>


Find Non-Ascii characters in MySQL
According to wikipedia "The American Standard Code for Information Interchange (ASCII /ˈæski/ ass-kee) is a character-encoding scheme originally based on the English alphabet that encodes 128 specified characters - the numbers 0-9, the letters a-z and A-Z, some basic punctuation symbols, some control codes that originated with Teletype machines, and a blank space - into the 7-bit binary integers".
To find non ASCII characters from a MySQL table you can use the following query with a regular expression. This regular expression ([A-Za-z0-9.,-]) shows all characters except a-z, A-Z, 0-9, periods, commas, and hyphens. You can modify the regular expression as per your requirement.
SQL Query:
SELECT * FROM table_name WHERE NOT column_to_check REGEXP '[A-Za-z0-9.,-]';
Let see the following MySQL statements .
mysql> CREATE TABLE TEST(test_char varchar(8));
Query OK, 0 rows affected (0.60 sec)
mysql> INSERT INTO test VALUES('abcd');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO test VALUES('æ');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO test VALUES('123xyz');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO test VALUES('É');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO TEST VALUES('--');
Query OK, 1 row affected (0.05 sec)
mysql> SELECT * FROM TEST;
+-----------+
| test_char |
+-----------+
| abcd      |
| æ         |
| 123xyz    |
| É         |
| --        |
+-----------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM TEST WHERE NOT test_char REGEXP '[A-Za-z0-9.,-]';
+-----------+
| test_char |
+-----------+
| æ         |
| É         |
+-----------+
2 rows in set (0.00 sec)

0 comments:

Post a Comment