Found 6703 Articles for Database

How to easily insert datetime in MySQL?

AmitDiwan
Updated on 04-Oct-2023 21:39:34

25K+ Views

You can easily insert DateTime with the MySQL command line. Following is the syntax − insert into yourTableName values(‘yourDateTimeValue’); Let us first create a table − mysql> create table DemoTable (    DateOfBirth datetime ); Query OK, 0 rows affected (0.97 sec) Insert some records in the table using insert command − mysql> insert into DemoTable values('1998-01-23 12:45:56'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('2010-12-31 01:15:00'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2015-04-03 14:00:45'); Query OK, 1 row affected (0.10 sec) Display all records from the table using select statement ... Read More

More elegant way to insert empty java.sql.Date in MySQL Database?

AmitDiwan
Updated on 26-Sep-2019 08:31:50

309 Views

Let us first create a table. Following is the query to create a table in database ‘web’ −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.53 sec)Here is the Java code to insert empty(NULL) java.sql.Date in MySQL DB −import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertNullDateDemo{    public static void main(String[] args){       Connection con=null;       PreparedStatement ps=null;       try{          con=DriverManager.getConnection("jdbc:mysql://localhost:3306/web?useSSL=false", "root", "123456");          String query="insert into DemoTable(AdmissionDate) values(?) ";          ps= con.prepareStatement(query);         ... Read More

Remove trailing numbers surrounded by parenthesis from a MySQL column

AmitDiwan
Updated on 26-Sep-2019 08:26:46

135 Views

For this, use trim() along with substring(). Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('1stJohn'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('1stJohn (7)'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable values('2ndSam'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2ndSam (4)'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will ... Read More

MySQL query to group concat distinct by Id?

AmitDiwan
Updated on 26-Sep-2019 08:24:55

379 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable values(200, 'Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(300, 'David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(400, 'Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

MySQL query to replace backslash from a varchar column with preceding backslash string values

AmitDiwan
Updated on 26-Sep-2019 08:22:18

1K+ Views

Let us first create a table −mysql> create table DemoTable (    Title varchar(100) ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('\"MySQL'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('MongoDB\"'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('\"Java\"'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('\"C\"'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | Title     ... Read More

Is it necessary to add DEFAULT NULL in MySQL?

AmitDiwan
Updated on 26-Sep-2019 08:19:44

2K+ Views

No, it isn’t necessary because without adding DEFAULT NULL, it gives NULL value. For example, let’s say you haven’t added DEFAULT NULL and inserted a record with no value, then the result would display the NULL value as the inserted value.Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100) ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(); Query ... Read More

MySQL query to remove everything except the last 7 characters in column record

AmitDiwan
Updated on 26-Sep-2019 08:16:19

80 Views

Let us first create a table −mysql> create table DemoTable (    Strength text ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John is a good programmer'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Chris is a good player in cricket'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Bob is good in algorithm'); Query OK, 1 row affected (0.42 sec)Display all records from the table using select statement −mysql> select *from DemoTableThis will produce the following output −+-----------------------------------+ | Strength ... Read More

Implement CASE statement with WHEN clause to check for the existence of a record in MySQL

AmitDiwan
Updated on 26-Sep-2019 08:14:59

140 Views

Let us first create a table −mysql> create table DemoTable (    UserId int,    UserName varchar(100) ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(101, 'Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(103, 'Mike'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

How can I achieve similar result like using a loop in MySQL WHERE clause to display only alternate ID records?

AmitDiwan
Updated on 26-Sep-2019 08:13:29

101 Views

Get similar results using MySQL IN(). Let us first create a table −mysql> create table DemoTable (    ClientId int,    ClientName varchar(100),    ClientAge int ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris', 34); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(101, 'Robert', 31); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(103, 'David', 33); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(104, 'Mike', 45); Query OK, 1 row affected (0.17 sec) mysql> insert ... Read More

MySQL query to order by current day and month?

AmitDiwan
Updated on 26-Sep-2019 08:10:26

235 Views

For this, you can use the ORDER BY CASE statement. Let us first create a table −mysql> create table DemoTable (    DueDate date ); Query OK, 0 rows affected (0.56 sec)Note − Let’s say the current date is 2019-07-22.Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-03-10'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-08-25'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-06-01'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2019-12-31'); Query OK, 1 row affected (0.17 sec)Display all records from the ... Read More

Advertisements