Tuesday, 10 July 2018

Mysql UPPER() function

Mysql UPPER() function

MySQL UPPER() converts all the characters in a string to uppercase characters.
Syntax:
UPPER (string)
Argument
NameDescription
stringA string whose characters are to be converted to uppercase.Example : MySQL UPPER() function
The following MySQL statement returns the given string after converting all of its characters in uppercase.
Code:
SELECT UPPER('myteststring');


Sample Output:
mysql> SELECT UPPER('myteststring');
+-----------------------+
| UPPER('myteststring') |
+-----------------------+
| MYTESTSTRING          | 
+-----------------------+
1 row in set (0.00 sec)
Example of MySQL UPPER() function with where clause
The following MySQL statement returns those rows from the publisher table whose publishers are not belonging the country USA. The column pub_name, and values of publisher names in uppercase have been shown in the output.
Code:
SELECT pub_name,UPPER(pub_name) 
FROM publisher 
WHERE country<>'USA'; 


Sample table: publisher

Sample Output:
mysql> SELECT pub_name,UPPER(pub_name) 
    -> FROM publisher 
    -> WHERE country<>'USA';
+------------------------------+------------------------------+
| pub_name                     | UPPER(pub_name)              |
+------------------------------+------------------------------+
| BPP Publication              | BPP PUBLICATION              | 
| New Harrold Publication      | NEW HARROLD PUBLICATION      | 
| Ultra Press Inc.             | ULTRA PRESS INC.             | 
| Pieterson Grp. of Publishers | PIETERSON GRP. OF PUBLISHERS | 
| Novel Publisher Ltd.         | NOVEL PUBLISHER LTD.         | 
+------------------------------+------------------------------+
5 rows in set (0.11 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-upper-function - php mysql examples | w3resource</title>
<meta name="description" content="example-upper-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 Publishers those who belong to USA. Second column shows name of the Publisher in upper case:</h2>
<table class='table table-bordered'>
<tr>
<th>Publishers name</th><th>UPPER(pub_name)</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_name,UPPER(pub_name) 
FROM publisher 
WHERE country<>"USA"') as $row) {
echo "<tr>";
echo "<td>" . $row['pub_name'] . "</td>";
echo "<td>" . $row['UPPER(pub_name)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>



0 comments:

Post a Comment