AmitDiwan has Published 11365 Articles

Update all rows in MySQL and remove all the unnecessary whitespaces in and around the string?

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 11:01:29

63 Views

To remove unnecessary whitespaces, use TRIM() in MySQL. Let us first create a −mysql> create table DemoTable1405    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert −mysql> insert into DemoTable1405 values(' ... Read More

Display month names and year from a column with date records with MySQL

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:57:08

379 Views

Let us first create a −mysql> create table DemoTable1619    -> (    -> ArrivalTime datetime    -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert −mysql> insert into DemoTable1619 values(now()); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable1619 values(curdate()); ... Read More

MySQL query to copy IP address from varchar column to integer in the same table?

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:55:05

192 Views

For this, you can use INET_ATON(). Let us first create a −mysql> create table DemoTable1404    -> (    -> IpAddress varchar(40)    -> ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert −mysql> insert into DemoTable1404 values('192.168.120.0'); Query OK, 1 row affected (0.43 ... Read More

Update table and order dates in MySQL

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:52:35

320 Views

You cannot use UPDATE command with ORDER BY clause, but you can use SELECT statement with ORDER BY DESC.Let us first create a −mysql> create table DemoTable1403    -> (    -> DueDate timestamp    -> ); Query OK, 0 rows affected (1.26 sec)Insert some records in the table using ... Read More

Display substring in MySQL if the string is less than a specific length or display a custom message if it is more?

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:50:57

113 Views

For this, you can use substring() function in MySQL. For conditions, use MySQL CASE statement. Let us first create a −mysql> create table DemoTable1402    -> (    -> EmployeeName varchar(40)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert −mysql> insert ... Read More

Why do backticks won’t work in the SET part of an UPDATE query (for MySQL)?

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:48:35

118 Views

Backticks would work if written correctly as in the below syntax −update `yourTableName` set `yourTableName`.`yourColumnName`='yourNewValue' where yourCondition;Let us first create a −mysql> create table `DemoTable_1401`    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert −mysql> ... Read More

How to implement CANDIDATE key in any MySQL table?

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:47:16

3K+ Views

Each relation may have one or more candidate key. One of these candidate keys is called Primary Key. Each candidate key qualifies for Primary Key. Therefore, candidates for Primary Key is called Candidate Key. To implement candidate key in MySQL, set more than one column as unique key. These keys ... Read More

Getting the next primary key without adding a new record is impossible, isn't it in MYSQL

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:45:43

42 Views

No, it is possible to get the next primary key without adding a new record. Let us first create a −mysql> create table DemoTable1399    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records ... Read More

Find percentage from marks in MySQL

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:42:56

354 Views

Let us first create a −mysql> create table DemoTable1398    -> (    -> Marks int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert −mysql> insert into DemoTable1398 values(78); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1398 values(82); ... Read More

Comparison of varchar date records from the current date in MySQL

AmitDiwan

AmitDiwan

Updated on 11-Nov-2019 10:41:10

532 Views

For date comparison, you can use STR_TO_DATE(). Following is the syntax −select * from yourTableName where str_to_date(yourColumnName, 'yourFormatSpecifier') > curdate();Let us first create a −mysql> create table DemoTable1397    -> (    -> AdmissionDate varchar(40)    -> );s Query OK, 0 rows affected (0.97 sec)Insert some records in the table ... Read More

Advertisements