AmitDiwan has Published 10740 Articles

Convert DATE timestamp to return only the month name in MySQL

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:35:56

237 Views

To return only the month name, you can use DATE_FORMAT() −mysql> create table DemoTable1999 (    ArrivalDate timestamp ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1999 values('2019-01-01 12:34:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into ... Read More

Return only a single row from duplicate rows with MySQL

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:33:52

1K+ Views

To return only a single row from duplicate rows, use DISTINCT keyword −mysql> create table DemoTable1998 (    Name varchar(20) ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1998 values('Robert'); Query OK, 1 row affected (0.17 sec) mysql> insert ... Read More

MySQL - Convert YYYY-MM-DD to UNIX timestamp

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:32:04

867 Views

To convert date to UNIX timestamp, use UNIX_TIMESTAMP() in MySQL −mysql> create table DemoTable1997 (    DueDate date ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1997 values('2018-10-11'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1997 ... Read More

Display distinct column name in MySQL

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:29:29

245 Views

Let us create a table −mysql> create table DemoTable1996 (    ShippingDate datetime,    CustomerName varchar(20) ); Query OK, 0 rows affected (0.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1996 values('2019-12-21 10:45:00', 'Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1996 ... Read More

Add 11 days to current date in MySQL

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:27:36

446 Views

Let us first create a table −mysql> create table DemoTable1994 (    ArrivalDate date ); Query OK, 0 rows affected (5.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1994 values('2019-12-18'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable1994 values('2019-12-19'); Query OK, 1 ... Read More

Set custom messages for enum values in MySQL

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:25:01

213 Views

Use the if else to set custom messages for enum. Let us first create a table −mysql> create table DemoTable1992 (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(20),    isActive ENUM('Y', 'N') ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using ... Read More

Select from table where value does not exist with MySQL?

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:22:00

896 Views

For this, you can use NOT IN() −mysql> create table DemoTable1991 (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20) ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1991(StudentName) values('Chris'); Query OK, 1 row affected (0.24 ... Read More

MySQL - Select dates that are one week ahead from today?

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:19:12

362 Views

To get dates that are one week ahead from today, use DATEDIFF. Let us first get the current date −mysql> select curdate(); +------------+ | curdate()  | +------------+ | 2019-12-20 | +------------+ 1 row in set (0.00 sec)We will first create a table −mysql> create table DemoTable1990    (    ShippingDate ... Read More

Convert VARCHAR data to MySQL date format?

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:17:18

764 Views

To convert VARCHAR data to date format, you can use STR_TO_DATE() −mysql> create table DemoTable1989    (    DueDate varchar(20)    ); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1989 values('31/01/2015'); Query OK, 1 row affected (0.52 sec) mysql> ... Read More

Set an alternative of WHERE clause for each SELECT field in MySQL

AmitDiwan

AmitDiwan

Updated on 02-Jan-2020 05:15:50

532 Views

You can use CASE statement −mysql> create table DemoTable1988    (    Value1 int,    Value2 int,    Price int    ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1988 values(10, 7, 500); Query OK, 1 row affected (0.21 ... Read More

Advertisements