Easiest Way to Insert Date Records in MySQL

AmitDiwan
Updated on 17-Dec-2019 06:37:37

246 Views

Use the STR_TO_DATE() method to insert date records as in the below syntax −select str_to_date(yourColumnName, '%b %Y') from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> JoiningYear varchar(20)    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Jan 2018'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('May 2107'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Aug 2019'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Oct 2020'); Query OK, ... Read More

Implement MySQL REGEXP to Fetch Records with Numbers

AmitDiwan
Updated on 17-Dec-2019 06:33:48

136 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Version varchar(20)    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('1.0.0'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('2.s6.9'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('1.5.0'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------+ | Version | +---------+ | 1.0.0   | | 2.s6.9  | ... Read More

MySQL Query to Set Currency Records

AmitDiwan
Updated on 17-Dec-2019 06:31:26

269 Views

Use FORMAT() in MySQL to display currency records and display them in the correct form. Let us first create a table −mysql> create table DemoTable    -> (    -> Amount DECIMAL(15, 4)    -> ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(90948484); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(1000000000); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(1535353536); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(773646463); Query OK, 1 row affected (0.20 sec)Display all ... Read More

MySQL Query to Display Only 15 Words from the Left

AmitDiwan
Updated on 17-Dec-2019 06:29:59

91 Views

For this, use LEFT in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Title text    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Java database connectivity to MySQL database'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Python with django framework'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('C with data structure and algorithm'); Query OK, 1 row affected (0.33 sec)Display all records from the table using select statement −mysql> select ... Read More

Replacing Numbers on a Comma Delimited Result with MySQL

AmitDiwan
Updated on 17-Dec-2019 06:27:59

257 Views

For this, use CASE statement along with FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable1629     -> (     -> Month varchar(100)     -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command.mysql> insert into DemoTable1629 values('2, 4, 6'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1629 values('1, 3, 5, 12'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1629 values('7, 8, 9, 10'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement − mysql> select ... Read More

Create MySQL DATETIME Column with Default 0000-00-00

AmitDiwan
Updated on 17-Dec-2019 06:27:12

809 Views

To set default, you can use the DEFAULT keyword in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(20),    -> EmployeeJoiningDate datetime default '0000-00-00 00:00:00'    -> )ENGINE=MyISAM, AUTO_INCREMENT=100; Query OK, 0 rows affected (0.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(EmployeeName) values('David'); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(EmployeeName) values('Mike'); Query OK, 1 row affected (0.03 sec) mysql> ... Read More

Get Single Value from Comma-Separated String in MySQL

AmitDiwan
Updated on 17-Dec-2019 06:19:15

514 Views

For this, use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable1615    -> (    -> ListOfSubject text    -> ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1615 values('Python, Java, MySQL, MongoDB, C, C++, ASP.net'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select * from DemoTable1615; This will produce the following output −+-----------------------------------------+ | ListOfSubject                           | +-----------------------------------------+ | Python, Java, MySQL, MongoDB, C, ... Read More

Get Three Records Having Higher Value from MySQL

AmitDiwan
Updated on 17-Dec-2019 06:15:23

103 Views

Let us first create a table −mysql> create table DemoTable1614    -> (    -> StudentName varchar(20),    -> StudentScore int    -> ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1614 values('Adam', 65); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1614 values('Chris', 89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1614 values('Bob', 58); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1614 values('Sam', 98); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1614 values('Mike', 87); Query OK, 1 ... Read More

Function Similar to Oracle's TRUNC(SYSDATE) in MySQL

AmitDiwan
Updated on 17-Dec-2019 06:13:01

862 Views

Yes, you can use DATE() to get only date part in MySQL and you can use CURDATE() to get the current date in MySQL.The current date is as follows −mysql> select curdate(); +------------+ | curdate()  | +------------+ | 2019-10-20 | +------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable1613    -> (    -> PostingDate datetime    -> ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1613 values('2019-10-20 12:02:45'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1613 values('2018-10-20 12:02:45'); ... Read More

Get the Size of Selected Rows in MySQL

AmitDiwan
Updated on 17-Dec-2019 06:09:47

2K+ Views

To get the size of selected rows, use CHAR_LENGTH(). Let us first create a table −mysql> create table DemoTable1612    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1612 values('David', 'Brown'); Query OK, 1 row affected (0.75 sec) mysql> insert into DemoTable1612 values('John', 'Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1612 values('Bob', 'Taylor'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select * from DemoTable1612;This ... Read More

Advertisements