AmitDiwan has Published 10740 Articles

MySQL query to fetch records where decimal is a whole number

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 11:24:49

480 Views

For this, use FLOOR() function. Here, we will be fetching records like 12.00, 35.00, etc. from a list with records like 5.23, 8.76, 12.00, 22.68, etc. Let us first create a table −mysql> create table DemoTable (    Value DECIMAL(4, 2) ); Query OK, 0 rows affected (0.53 sec)Insert some ... Read More

Find the percentage of a student on basis of marks and add percent sign (%) to the result in SQL

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 11:23:05

2K+ Views

Let us first create a table −mysql> create table DemoTable (    Marks int ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(65); Query OK, 1 ... Read More

MySQL query to delete a row if two columns are equal

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 11:20:38

510 Views

Use DELETE for this. Let us first create a table −mysql> create table DemoTable (    Name varchar(40),    Score1 int ,    Score2 int ); Query OK, 0 rows affected (2.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 56, 76); Query OK, ... Read More

How to get the difference between date records and the current date in MySQL?

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 11:16:46

231 Views

Let’s say the current date is 2019-09-06. For our example, we will first create a table −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-08'); Query OK, 1 row affected ... Read More

Update a column of text with MySQL REPLACE()

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 10:52:50

928 Views

Let us first create a table −mysql> create table DemoTable (    Code varchar(100) ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('8565-9848-7474'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('9994-6464-8737'); Query OK, 1 ... Read More

How to convert MySQL DATETIME value to JSON format in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 10:49:22

687 Views

To convert, use JSON.stringify(). Following is the code to convert MySQL DATETIME value to JSON format in JavaScript − var mySQLDateTime = new Date("Fri Sep 06 2019 22 −54 −48 "); var yearValue = mySQLDateTime.getFullYear(); var dateValue = mySQLDateTime.getDate(); var monthValue=mySQLDateTime.getMonth(); var hour=mySQLDateTime.getHours(); var minutes=mySQLDateTime.getMinutes(); var second=mySQLDateTime.getSeconds(); jsonObject={"year" −yearValue, "month" ... Read More

MySQL SELECT query to return records with specific month and year

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 10:45:23

1K+ Views

For specific month, use MONTH() and for year, use YEAR() method. Let us first create a table −mysql> create table DemoTable (    StudentName varchar(40),    StudentAdmissionDate date ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', '2019-01-21'); ... Read More

MySQL query to return a string as a result of IF statement?

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 10:42:43

528 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeSalary int ); Query OK, 0 rows affected (1.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeSalary) values(12000); Query OK, 1 row affected (0.24 sec) ... Read More

How to update column values with date records and set 1 for corresponding records before the current date in SQL

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 10:40:03

314 Views

Let’s say the current date is 2019-08-20. Now for our example, we will create a table −mysql> create table DemoTable (    ProductStatus tinyint(1),    ProductExpiryDate date ); Query OK, 0 rows affected (1.03 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(0, '2019-06-12'); Query ... Read More

MySQL query for grouping and summing the values based on specific records

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 08:42:26

123 Views

Use GROUP BY to group records, whereas SUM() function is used to add. Let us first create a table −mysql> create table DemoTable (    Name varchar(40),    Subject varchar(40),    Marks int ); Query OK, 0 rows affected (2.89 sec)Insert some records in the table using insert command −mysql> ... Read More

Advertisements