w3resource
MYSQL Tutorial

MySQL BIT_LENGTH() function

BIT_LENGTH() function

MySQL BIT_LENGTH() returns the length of the given string in bits.

Syntax:

BIT_LENGTH (str1)

Argument

Name Description
str1 A string whose BIT_LENGTH value is to be retrieved.

Video Presentation

Your browser does not support HTML5 video.

Example: MySQL BIT_LENGTH() function

The following MySQL statement will return the length of the given string 'my text' in bits, i.e. 56.

Code:

SELECT BIT_LENGTH('my text'); 

Sample Output:

mysql> SELECT BIT_LENGTH('my text'); 
+-----------------------+
| BIT_LENGTH('my text') |
+-----------------------+
|                    56 | 
+-----------------------+
1 row in set (0.02 sec)

Example of MySQL BIT_LENGTH() function using table

The following MySQL statement will return the length of each city mentioned in pub_city column in bits.

Code:

SELECT pub_city, 
BIT_LENGTH(pub_city)  AS 'bit length' 
FROM publisher; 

Sample table: publisher


Sample Output:

mysql> SELECT pub_city, 
    -> BIT_LENGTH(pub_city)  AS 'bit length' 
    -> FROM publisher; 
+-----------+------------+
| pub_city  | bit length |
+-----------+------------+
| New York  |         64 | 
| Mumbai    |         48 | 
| Adelaide  |         64 | 
| London    |         48 | 
| Houstan   |         56 | 
| New York  |         64 | 
| Cambridge |         72 | 
| New Delhi |         72 | 
+-----------+------------+
8 rows in set (0.02 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-bit_length-function - php MySQL examples | w3resource</title>
 <meta name="description" content="example-bit_length-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 publisher's city and its bit length: </h2>
 <table class='table table-bordered'>
 <tr>
 <th>Publisher's city</th><th>Bit length</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 pub_city,BIT_LENGTH(pub_city)
AS "bit length"
FROM publisher') as $row) {
echo "<tr>";
echo "<td>" . $row['pub_city'] . "</td>";
echo "<td>" . $row['bit length'] . "</td>";
echo "</tr>"; 
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

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-bit_length-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 pub_city,BIT_LENGTH(pub_city) AS 'bit length' FROM publisher";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Publisher's city</td>
<td>Bit length</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("pub_city")%></TD>
<TD><%=rs.getString("bit length")%></TD>
</TR>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

All String Functions

MySQL String Functions, slide presentation


ankara escort
kızılay escort