Found 4378 Articles for MySQL

How to select month and year from dates in MySQL?

Sharon Christine
Updated on 30-Jun-2020 09:49:03

392 Views

To select month and year in MySQL, you can use MONTH() and YEAR() method. Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate datetime    -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('2019-06-15'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2018-12-01'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2015-04-01'); Query OK, 1 row affected (0.16 sec) ... Read More

How to extract the digit part from the string in MySQL?

Sharon Christine
Updated on 30-Jun-2020 09:50:34

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John19383'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Carol9999'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David123456'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------+ | StudentId   | +-------------+ | John19383 ... Read More

Implement multiple COUNT() in a single MySQL query

karthikeya Boyini
Updated on 30-Jun-2020 09:31:00

671 Views

For this, use CASE statement. Let us first create a table −mysql> create table DemoTable    -> (    -> LastName varchar(100)    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Taylor'); Query OK, 1 ... Read More

Can we use PRIMARY KEY( column1, column2) in MySQL to make pairs?

karthikeya Boyini
Updated on 30-Jun-2020 09:33:04

120 Views

Yes, you can use below syntax. Following is the syntax −PRIMARY KEY(yourColumnName1, yourColumnName2);Let us first create a table −mysql> create table DemoTable    -> (    -> StudentFirstName varchar(100),    -> StudentLastName varchar(100),    -> StudentAge int,    -> StudentCountryName varchar(100),    -> PRIMARY KEY(StudentFirstName, StudentLastName)    -> ); Query OK, 0 rows affected (0.74 sec)Let us check the description of the table −mysql> desc DemoTable;OutputThis will produce the following output −+--------------------+--------------+------+-----+---------+-------+ | Field              | Type         | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+-------+ | StudentFirstName   | ... Read More

Can we use semicolon as a MySQL DEMILITER?

karthikeya Boyini
Updated on 30-Jun-2020 09:33:49

87 Views

No, we cannot. If you still did it, then stored procedure won’t get created. Therefore, first you need to change your DELIMITER from semicolon(;) to others like (// ,??..etc). Following is the syntax −DELIMITER // CREATE PROCEDURE yourProcedureName() BEGIN yourStatement1, . . . . N END // DELIMITER ;Let us implement the above syntax in order to create a stored procedure −mysql> DELIMITER // mysql> CREATE PROCEDURE get_Message()    -> BEGIN    -> SELECT CONCAT("HELLO", " ", "MYSQL USERS");    -> END    -> // Query OK, 0 rows affected (0.23 sec) mysql> DELIMITER ;Now you can call the stored ... Read More

Retrieving MySQL Database structure information from Java?

Sharon Christine
Updated on 30-Jul-2019 22:30:26

338 Views

Use DatabaseMetaData class to retrieve MySQL database structure. In this example, we will display all the table names of database “web” using Java with the help of getMetaData().Following is the Java code −Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.DatabaseMetaData; public class getDatabaseInformationDemo {    public static void main(String[] args) {       Connection con = null;       try {          con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web?useSSL=false", "root", "123456");          DatabaseMetaData information = (DatabaseMetaData) con.getMetaData();          String allTableName[] = {             "TABLE"   ... Read More

Fetch middle part of a string surrounded by slash in MySQL

Sharon Christine
Updated on 30-Jun-2020 09:36:30

367 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Code varchar(100)    -> ); Query OK, 0 rows affected (1.07 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('/101/102/106'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('/110/111/101'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('/111/114/201'); Query OK, 1 row affected (0.47 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------------+ | Code         | +--------------+ | ... Read More

MySQL queries to update date records with NULL values

Sharon Christine
Updated on 30-Jul-2019 22:30:26

809 Views

You can use IFNULL() for this. Let us first create a table −mysql> create table DemoTable -> ( -> added_date date, -> updated_date date -> ); Query OK, 0 rows affected (0.95 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-10', '2019-06-01'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-05-19', NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL, '2019-09-05'); Query OK, 1 row affected (0.18 sec)Display all records from the table using ... Read More

How to use special characters in column names with MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 09:38:05

2K+ Views

Using backticks around the column name will allow you to use special characters. Let us first create a table −mysql> create table DemoTable    -> (    -> `Student-Id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> `Student-Name` varchar(100),    -> `Student-Age` int    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(`Student-Name`, `Student-Age`) values('Chris', 21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(`Student-Name`, `Student-Age`) values('Mike', 19); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(`Student-Name`, `Student-Age`) values('Bob', 18); Query OK, ... Read More

Set format specifier in MySQL STR_TO_DATE() and convert string to date

karthikeya Boyini
Updated on 30-Jun-2020 09:39:31

207 Views

Specify format specifier in STR_TO_DATE() method that convertes string to date. Following is the syntax −select STR_TO_DATE(yourColumnName, 'yourFormatSpecifier') from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> AdmissionDate varchar(100) -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('01-12-2019'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('15-06-2019'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('21-01-2016'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from ... Read More

Advertisements