Tuesday 10 July 2018

Mysql LPAD() function

Mysql LPAD() function

MySQL LPAD() left pads a string with another string. The actual string, a number indicating the length of the padding in characters (optional) and the string to be used for left padding - all are passed as arguments.
Syntax:
LPAD(str, len, padstr)
Arguments
NameDescription
strActual string.
lenA number indicating the total length of the string ( in characters) returned after padding.
padstrString which is used for left padding.
MySQL Version: 5.6
Pictorial Presentation
MySQL LPAD() pictorial presentation
Example : MySQL LPAD() function
In the following MySQL statement, the first argument specifies a string of 6 characters, the second argument specifies that the length of the string returned after padding will be 10 characters and the third argument specifies the string to be used for left padding. So, 4 characters (10-6) are used for left padding and the function thus returns '****Hellow.
Code:
SELECT LPAD('Hellow',10,'**');


Sample Output:
mysql> SELECT LPAD('Hellow',10,'**');
+------------------------+
| LPAD('Hellow',10,'**') |
+------------------------+
| ****Hellow             | 
+------------------------+
1 row in set (0.03 sec)
Example of LPAD() function with less than the original string
The following MySQL statement returns 'Hell'. This happens because, the first argument has 6 characters, second argument 4 is the total number of characters after left padding and the third argument is the padding string. Since a total number of characters after padding is less than the total number of characters in the first argument, so to meet the condition, two characters are omitted from the actual string (i.e. the first argument).
SELECT LPAD('Hellow',4,'**');


Sample Output:
mysql> SELECT LPAD('Hellow',4,'**');
+-----------------------+
| LPAD('Hellow',4,'**') |
+-----------------------+
| Hell                  | 
+-----------------------+
1 row in set (0.00 sec)
Example of LPAD() function using column of a table
The following MySQL statement returns all the rows from publisher table, with the value of column country left padded with the string ‘**’, while after padding, the length of the string returned will be a value obtained by subtracting the length of the country () from 25.
Code:
SELECT LPAD(country,25-(length(country)),'**')
FROM publisher;


Sample table: publisher

Sample Output:
mysql> SELECT LPAD(country,25-(length(country)),'**')
    -> FROM publisher;
+-----------------------------------------+
| LPAD(country,25-(length(country)),'**') |
+-----------------------------------------+
| *******************USA                  | 
| ***************India                    | 
| *******Australia                        | 
| *********************UK                 | 
| *******************USA                  | 
| *******************USA                  | 
| *********************UK                 | 
| ***************India                    | 
+-----------------------------------------+
8 rows in set (0.00 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-lpad-function - php mysql examples | w3resource</title>
<meta name="description" content="example-lpad-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>A list of the expression named as output. Where output is : left padded the column country with the string "**", after padding, the length of the string will be the subtraction of field size and the length of the content of the field.:</h2>
<table class='table table-bordered'>
<tr>
<th>Output</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  LPAD(country,25-(length(country)),"**") as output
FROM publisher') as $row) {
echo "<tr>";
echo "<td>" . $row['output'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>





Example of LPAD() function with where clause
The following MySQL statement returns all the rows from publisher table who is having the country name beginning with the letter ‘U’ and column country left padded with the string ‘**’. After padding, the length of the string will be the subtraction of field size and the length of the content of the field.
Code:
SELECT LPAD(country,25-(length(country)),'**')
FROM publisher
WHERE LEFT(country,1)=’U’;


Sample table: publisher

Sample Output:
mysql> SELECT LPAD(country,25-(length(country)),'**')
    -> FROM publisher
    -> WHERE LEFT(country,1)='U'; 
+-----------------------------------------+
| LPAD(country,25-(length(country)),'**') |
+-----------------------------------------+
| *******************USA                  | 
| *********************UK                 | 
| *******************USA                  | 
| *******************USA                  | 
| *********************UK                 | 
+-----------------------------------------+
5 rows in set (0.00 sec)
PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-lpad-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-lpad-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>A list of the expression named as output. Where output is the list of the countries who is having the country name beginning  with the letter ‘U’ and with left padded the column country with the string ‘**’. After padding, the length of the string, will be the subtraction of field size and the length of the content of the field:</h2>
<table class='table table-bordered'>
<tr>
<th>Output</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  LPAD(country,25-(length(country)),"**") as output
FROM publisher
WHERE LEFT(country,1)="U"') as $row) {
echo "<tr>";
echo "<td>" . $row['output'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

0 comments:

Post a Comment