Found 6702 Articles for Database

Get count of zeros for columns values declared with INT type in MySQL

AmitDiwan
Updated on 30-Sep-2019 06:52:26

189 Views

For this, you can use LENGTH() along with REPLACE(). Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10002000); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(00000); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(400560); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----------+ | Value    | +----------+ | 10002000 ... Read More

Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL

AmitDiwan
Updated on 30-Sep-2019 06:50:15

199 Views

For this, you can use IFNULL(). Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 'Doe'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values(NULL, 'Taylor'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David', NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(NULL, 'Miller'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL query to compare and display only the rows with NULL values

AmitDiwan
Updated on 30-Sep-2019 06:48:14

112 Views

For this, you can use IFNULL(). Let us first create a table −mysql> create table DemoTable (    Value1 int,    Value2 int ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, NULL); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20, 40); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(3, NULL); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+--------+ | Value1 | Value2 ... Read More

MySQL UNIQUE declaration to avoid inserting duplicate values?

AmitDiwan
Updated on 30-Sep-2019 06:46:13

195 Views

Following is the declaration for a UNIQUE clause in MySQL −create table yourTableName (    yourColumnName1 dataType,    yourColumnName2 dataType,    UNIQUE(yourColumnName1),    UNIQUE(yourColumnName1) );Let us first create a table −mysql> create table DemoTable (    Value int,    Value2 int,    UNIQUE(Value),    UNIQUE(Value2) ); Query OK, 0 rows affected (13.98 sec)Insert some records in the table using insert command. Here, duplicate records won’t insert because we have used UNIQUE above −mysql> insert into DemoTable values(10, 20) ; Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(10, 30); ERROR 1062 (23000): Duplicate entry '10' for key ... Read More

Display distinct dates in MySQL from a column with date records

AmitDiwan
Updated on 30-Sep-2019 06:44:28

418 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate datetime ); Query OK, 0 rows affected (8.99 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2010-01-10 12:30:45'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2010-01-10 12:30:45'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('2010-01-10 12:30:45'); Query OK, 1 row affected (0.71 sec) mysql> insert into DemoTable values('2010-01-12 12:30:45'); Query OK, 1 row affected (1.22 sec) mysql> insert into DemoTable values('2010-01-20 12:30:45'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2010-01-20 ... Read More

Check if a number is Palindrome in PL/SQLs

Arnab Chakraborty
Updated on 27-Sep-2019 10:42:03

7K+ Views

In this section we will see how to check whether a number is Palindrome or not using the PL/SQL. In PL/SQL code, some group of commands are arranged within a block of related declaration of statements.A number is palindrome if the number, and the reverse of that number are same. Suppose a number 12321, this is palindrome, but 12345 is not a palindrome.ExampleDECLARE    n number;    m number;    temp number:=0;    rem number; BEGIN    n :=12321;    m :=n;    while n>0    loop       rem := mod(n, 10);       temp := (temp*10)+rem; ... Read More

Get all the tables from a MySQL database having a specific column, let’s say xyz?

AmitDiwan
Updated on 27-Sep-2019 08:00:45

163 Views

Let’s say we have a database “web” and we need to get all the tables having a specific column ’StudentFirstName’.For this, below is the query −mysql> select myColumnName.table_name from information_schema.columns myColumnName where myColumnName.column_name = 'StudentFirstName' and table_schema='web';This will produce the following output −+---------------+ | TABLE_NAME | +---------------+ | demotable109 | | demotable297 | | demotable335 | | demotable395 | | demotable418 | | demotable425 | | demotable436 | +---------------+ 7 rows in set (0.14 sec)Therefore, the above tables have one of the column names as “StudentFirstName”.Let us check the description ... Read More

Convert MySQL timestamp to UNIX Timestamp?

AmitDiwan
Updated on 27-Sep-2019 07:58:58

1K+ Views

To convert MySQL timestamp to UNIX Timestamp, use the UNIX_TIMESTAMP(). Following is the syntax −select unix_timestamp(yourColumnName) from yourTableName;Let us first create a table −mysql> create table DemoTable(    Duetimestamp timestamp ); Query OK, 0 rows affected (2.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(now()); Query OK, 1 row affected (1.53 sec) mysql> insert into DemoTable values('2016-01-21 12:34:00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2018-05-01 02:00:00'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable values('2017-03-02 01:10:20'); Query OK, 1 row affected (10.19 sec)Display all records from ... Read More

How can I replace & with an ampersand in my MySQL database?

AmitDiwan
Updated on 27-Sep-2019 07:57:41

596 Views

To replace & with an ampersand, use MySQL REPLACE(). Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value varchar(100) ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values('@amp'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(Value) values('&'); Query OK, 1 row affected (1.09 sec) mysql> insert into DemoTable(Value) values('#amp'); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----+-------+ ... Read More

Find average on the basis of corresponding duplicate VARCHAR values in MySQL

AmitDiwan
Updated on 27-Sep-2019 07:56:21

67 Views

Let us first create a table −mysql> create table DemoTable(    Value int,    Value2 varchar(100) ); Query OK, 0 rows affected (0.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, '999.999.999.999'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20, '888.888.888.888'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(30, '999.999.999.999'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+-----------------+ | Value | Value2 ... Read More

Advertisements