Found 4378 Articles for MySQL

How to convert string to time in MySQL?

Kumar Varma
Updated on 30-Jun-2020 13:01:06

2K+ Views

You can use format specifier. Following is the syntax −select str_to_date(yourColumnName, '%d/%m/%Y %h:%i %p') as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate varchar(100)    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('11/02/2019 10:35'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('21/12/2018 12:01'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------------------+ | DueDate   ... Read More

MySQL query to remove a value with only numbers in a column

Kumar Varma
Updated on 30-Jun-2020 13:05:28

746 Views

For this, you can use REGEXP. Let us first create a table −mysql> create table DemoTable    -> (    -> ClientCode varchar(100)    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris902'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Robert_'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('903'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('123_David'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement ... Read More

MySQL select for exact case sensitive match with hyphen in records

Rama Giri
Updated on 30-Jun-2020 13:06:30

484 Views

For exact case sensitive match, use BINARY after WHERE clause in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> EmployeeCode varchar(100)    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('EMP-1122'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('emp-1122'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values('EMP-6756'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('EMP-8775'); Query OK, 1 row affected (0.16 sec)Display all records ... Read More

Can we compare numbers in a MySQL varchar field?

Kumar Varma
Updated on 30-Jun-2020 13:07:42

995 Views

Yes, we can do this by first using CAST(). Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentScore varchar(100)    -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentScore) values('90'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(StudentScore) values('100'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentScore) values('56'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentScore) values('98'); Query OK, 1 ... Read More

How do I multiply an unsigned int by -1 on a MySQL SELECT?

Rama Giri
Updated on 30-Jun-2020 12:47:59

88 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------+ | Value | +-------+ |    10 | | ... Read More

Multiple column sorting in MySQL?

Kumar Varma
Updated on 30-Jun-2020 12:48:49

112 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Value int    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 85885); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(101, 885995474); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100, 895943); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------+-----------+ | Id ... Read More

MySQL query to fetch records wherein timestamp is before 15+ days?

Rama Giri
Updated on 30-Jun-2020 12:49:39

210 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> ArrivalDate datetime    -> ); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command. Let’s say the current date is 2019-07-03 −mysql> insert into DemoTable values('2019-07-03'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-06-20'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-06-15'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2018-06-11'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2018-06-01'); Query ... Read More

MySQL query to display structure of a table

Kumar Varma
Updated on 30-Jun-2020 12:50:19

1K+ Views

To display structure of a table, following is the syntax −show create table yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeFirstName varchar(100),    -> EmployeeLastName varchar(100),    -> EmployeeAge int,    -> isMarried tinyint(1),    -> EmployeeAddress varchar(100),    -> EmployeeCountryName varchar(100)    -> ); Query OK, 0 rows affected (0.62 sec)Here is the query to display structure −mysql> show create table DemoTable;OutputThis will produce the following output −+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table        | Create Table             ... Read More

How to select the sum of the column values with higher value in reach row with MySQL?

Rama Giri
Updated on 30-Jun-2020 12:51:01

97 Views

Use the CASE statements and set conditions for the same. Let us first create a table −mysql> create table DemoTable    -> (    -> X int,    -> Y int    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20, 30); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(40, 15); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(80, 85); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select ... Read More

Copy a few columns from a table to another in MySQL

karthikeya Boyini
Updated on 30-Jun-2020 12:52:17

286 Views

Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int,   -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10, 'John'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1 values(11, 'Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1 values(12, 'Robert'); Query OK, 1 row affected (0.32 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------+--------+ | Id ... Read More

Advertisements