Remove Decimal from Records and Sum Them Using a MySQL Query

AmitDiwan
Updated on 03-Sep-2019 12:38:48

484 Views

Use aggregate function SUM() along with REPLACE(). Let us first create a table −mysql> create table DemoTable857(Price varchar(100)); Query OK, 0 rows affected (1.40 sec)Insert some records in the table using insert command −mysql> insert into DemoTable857 values('50.60'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable857 values('40.30.20'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable857 values('10.20'); Query OK, 1 row affected (0.69 sec)Display all records from the table using select statement −mysql> select *from DemoTable857;This will produce the following output −+----------+ | Price    | +----------+ | 50.60    | | 40.30.20 | | ... Read More

Create a Table from View in MySQL

AmitDiwan
Updated on 03-Sep-2019 12:37:49

611 Views

To create a table from the view below is the syntax −create table yourTableName select *from yourViewName;Let us first create a table −mysql> create table DemoTable830(Name varchar(100)); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command −mysql> insert into DemoTable830 values('Chris'); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable830 values('Robert'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable830 values('David'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable830 values('Mike'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select ... Read More

MySQL Left Padding with Zeros Separated by Hyphen

AmitDiwan
Updated on 03-Sep-2019 12:28:57

174 Views

Let us first create a table −mysql> create table DemoTable829(SerialNumber int); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable829 values(100); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable829 values(101); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable829 values(102); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable829 values(103); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable829 values(104); Query OK, 1 row affected (0.60 sec)Display all records from the table using select statement −mysql> select *from DemoTable829;This will produce ... Read More

Display Substrings of Different Length with a Single MySQL Query

AmitDiwan
Updated on 03-Sep-2019 12:26:31

133 Views

Let us first create a table −mysql> create table DemoTable856(Title text); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable856 values('Introduction to MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable856 values('Java in depth'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable856 values('C++ with Data Structure'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable856;This will produce the following output −+-------------------------+ | Title                   | +-------------------------+ ... Read More

Remove Percentage Sign from Values in MySQL Table

AmitDiwan
Updated on 03-Sep-2019 12:24:31

400 Views

Let us first create a table −mysql> create table DemoTable828(    CustomerName varchar(100),    InterestRate varchar(100) ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable828 values('Chris', '10%'); Query OK, 1 row affected (0.60 sec) mysql> insert into DemoTable828 values('Robert', '11.5%'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable828 values('David', '15.90%'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable828 values('Bob', '20%'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable828;This will produce the following ... Read More

Check If a Timestamp is Set in MySQL

AmitDiwan
Updated on 03-Sep-2019 12:22:57

379 Views

Let us first create a table −mysql> create table DemoTable855(DueDate timestamp); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable855 values('2019-08-07'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable855 values('0000-00-00'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable855 values('2019-07-06'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable855 values('2016-04-10'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable855;This will produce the following output −+----------------------+ | DueDate             ... Read More

Replace Backslashes in Folder Path Using MySQL Query

AmitDiwan
Updated on 03-Sep-2019 12:21:28

287 Views

To replace backslashes with any other special character, use the REPLACE() method. Let us first create a table −mysql> create table DemoTable827(Path text); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable827 values('C\MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable827 values('D\NewFolder\'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable827 values('E:\myFolder\MyDocument'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable827;This will produce the following output −+------------------------+ | Path ... Read More

Sum of Previous Row Value with Current Row in MySQL Cross Join

AmitDiwan
Updated on 03-Sep-2019 12:20:14

1K+ Views

Let us first create a table −mysql> create table DemoTable(Value int); Query OK, 0 rows affected (1.79 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.68 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | +-------+ | 50    | | 20    | | 30    | +-------+ 3 rows in ... Read More

MySQL Query to Display Column Values with Whitespace

AmitDiwan
Updated on 03-Sep-2019 12:19:19

253 Views

For this, use the TRIM() function. Let us first create a table −mysql> create table DemoTable826(    FirstName varchar(100),    Age int ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using the insert command. Here, we have set the FirstName as whitespace for some of the records −mysql> insert into DemoTable826 values(' ', 24); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable826 values(' ', 22); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable826 values('Chris', 26); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable826 values('Robert', 27); Query ... Read More

Optimize MySQL Table After Deleting Rows

AmitDiwan
Updated on 03-Sep-2019 12:17:39

1K+ Views

Use the OPTIMIZE TABLE command to optimize a MySQL table −optimize table yourTableName;Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(100) ); Query OK, 0 rows affected (1.38 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.38 sec)Display all records from ... Read More

Advertisements