Functions in SQL: Manipulating Strings : String Function : String : SQL / MySQL examples (example source code) Organized by topic

C++
PHP
SQL / MySQL
SQL / MySQL Home »  String   » [  String Function  ]   
 



Functions in SQL: Manipulating Strings

/*

mysql> Select * from StudentExam;
+-----------+------+------------+
| StudentID | Mark | Comments   |
+-----------+------+------------+
|        10 |   46 | Java       |
|        10 |   65 | C#         |
|        10 |   79 | JavaScript |
|        11 |   66 | Java       |
|        11 |   85 | C#         |
|        11 |   99 | JavaScript |
+-----------+------+------------+
6 rows in set (0.00 sec)

mysql> /* Real command */
mysql> SELECT
    ->    CONCAT(RIGHT(Name, LENGTH(Name- INSTR(Name, ' '1),
    ->           ', ', LEFT(Name, INSTR(Name, ' '1))
    ->    AS StudentName
    -> FROM Student
    -> ORDER BY StudentName;
+---------------+
| StudentName   |
+---------------+
|  But, Cory    |
|  Harvests, JJ |
|  Yin, Joe     |
+---------------+
rows in set (0.04 sec)


*/

/* Prepare the data */ 
DROP TABLE Student;

CREATE TABLE Student (
   StudentID INT NOT NULL PRIMARY KEY,
   Name      VARCHAR(50NOT NULL
)TYPE = InnoDB;


/* Insert data for testing */ 
INSERT INTO Student (StudentID,NameVALUES (1,'Joe Yin');
INSERT INTO Student (StudentID,NameVALUES (2,'Cory But');
INSERT INTO Student (StudentID,NameVALUES (3,'JJ Harvests');

Select * from StudentExam;

  
/* Real command */
SELECT
   CONCAT(RIGHT(Name, LENGTH(Name- INSTR(Name, ' '1),
          ', ', LEFT(Name, INSTR(Name, ' '1))
   AS StudentName
FROM Student
ORDER BY StudentName;

           
       
Related examples in the same category
1.  Using String Functions in Your SQL Statements
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.