Tuesday, 10 July 2018

Mysql LCASE() function

Mysql LCASE() 

MySQL LCASE() converts the characters of a string to lower case characters.
This function is similar to the function LOWER().
Syntax:
LCASE (string)
Argument
NameDescription
stringA string whose characters are to be converted to lowercase characters.
MySQL Version: 5.6
Example of MySQL LCASE() function
The following MySQL statement converts all of the characters of the string 'MYTESTSTRING' to lower case characters and returns 'myteststring'.
Code:
SELECT LCASE('MYTESTSTRING');


Sample Output:
mysql> SELECT LCASE('MYTESTSTRING');
+-----------------------+
| LCASE('MYTESTSTRING') |
+-----------------------+
| myteststring          | 
+-----------------------+
1 row in set (0.00 sec)
Example of MySQL LCASE() function using where
The following MySQL statement returns those rows from the publisher table which does not contain a publisher belong to the USA. The column pub_name and pub_name in lowercase are displayed in the output.
Code:
SELECT pub_name,LCASE(pub_name) 
FROM publisher 
WHERE country<>'USA'; 


Sample table: publisher

Sample Output:
mysql> SELECT pub_name,LCASE(pub_name) 
    -> FROM publisher 
    -> WHERE country<>'USA';
+------------------------------+------------------------------+
| pub_name                     | LCASE(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.03 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-lcase-function - php mysql examples | w3resource</title>
 <meta name="description" content="example-lcase-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 don't belong to USA. Second column shows the name of the Publisher in lowercase:</h2>
 <table class='table table-bordered'>
 <tr>
 <th>Publishers name</th><th>Names in lowercase</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,LCASE(pub_name) FROM publisher WHERE country<>"USA" ') as $row) {
 echo "<tr>";
 echo "<td>" . $row['pub_name'] . "</td>";
 echo "<td>" . $row['LCASE(pub_name)'] . "</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-lcase-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_name,LCASE(pub_name) FROM publisher WHERE country<>'USA'";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Publishers name</td>
<td>Names in lowercase</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("pub_name")%></TD>
<TD><%=rs.getString("LCASE(pub_name)")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Cant connect to database.");
}
%>
</body>
</html>


MySQL: Upper case the first letter and lower case the rest of a string
We have a table called test with following records:
mysql> SELECT * FROM test;
+-----------+
| test_char |
+-----------+
| abcd      |
| WxyZ      |
| scott     |
| ROBIN     |
+-----------+
4 rows in set (0.00 sec)
Now we want to update the above data where the first character will be in upper case and rest will be lower case i.e. 'abcd' will be 'Abcd', 'WxyZ' will be 'Wxyz' and so on. See the following MySQL statement:
mysql> UPDATE TEST SET test_char = CONCAT(UCASE(LEFT(test_char, 1)), LCASE(SUBSTRING(test_char, 2)));
Query OK, 4 rows affected (0.04 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> SELECT * FROM TEST;
+-----------+
| test_char |
+-----------+
| Abcd      |
| Wxyz      |
| Scott     |
| Robin     |
+-----------+
4 rows in set (0.00 sec)

0 comments:

Post a Comment