MySQL Articles

Page 129 of 355

How to get the first day of next month in MySQL?

V Jyothi
V Jyothi
Updated on 29-Jan-2020 1K+ Views

With the help of following MySQL query, we can get the first day of next month −mysql> SELECT DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH,'%Y-%m-01') AS 'FIRST DAY OF NEXT MONTH'; +-------------------------+ | FIRST DAY OF NEXT MONTH | +-------------------------+ | 2017-11-01              | +-------------------------+ 1 row in set (0.00 sec)

Read More

How can we create a new MySQL table by selecting specific column/s from another existing table?

karthikeya Boyini
karthikeya Boyini
Updated on 29-Jan-2020 298 Views

As we know that we can copy the data and structure from an existing table by CTAS script. If we want to select some specific column/s from another table then we need to mention them after SELECT. Consider the following example in which we have created a table named EMP_BACKUP1 by selecting a specific column ‘name’ from already existing table ‘Employee’ −mysql> Create table EMP_BACKUP1 AS Select name from employee; Query OK, 3 rows affected (0.25 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> Select * from EMP_BACKUP1; +--------+ | name   | +--------+ | Ram    | | ...

Read More

How can we copy data with some condition/s from existing MySQL table?

Sai Nath
Sai Nath
Updated on 29-Jan-2020 308 Views

As we know that we can copy the data and structure from an existing table by CTAS script. If we want to copy data with some condition/s then we need to use WHERE clause with CTAS script. Consider the example below −mysql> Create table EMP_BACKUP2 AS SELECT * from EMPLOYEE WHERE id = 300 AND Name = 'Mohan'; Query OK, 1 row affected (0.14 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> Select * from EMP_BACKUP2; +------+-------+ | Id   | Name  | +------+-------+ | 300  | Mohan | +------+-------+ 1 row in set (0.00 sec)In the example ...

Read More

How can we get a list of columns in an existing MySQL table?

Arjun Thakur
Arjun Thakur
Updated on 29-Jan-2020 357 Views

Suppose if we forgot the names of the columns in an existing table then we can use SHOW COLUMNS statement as follows to get the list of columns −mysql> SHOW COLUMNS from Employee\G *************************** 1. row ***************************   Field: Id    Type: int(11)    Null: YES     Key: Default: NULL   Extra: *************************** 2. row ***************************   Field: Name    Type: varchar(20)    Null: YES     Key: Default: NULL   Extra: 2 rows in set (0.07 sec)In the example above, we got the list of columns of ‘Employee’ table with the help of SHOW COLUMNS statement.

Read More

How to get last day of the next month in MySQL?

Ramu Prasad
Ramu Prasad
Updated on 29-Jan-2020 427 Views

With the help of following MySQL query, we can get the last day of next month −mysql> SELECT LAST_DAY(now() + INTERVAL 1 MONTH) AS 'LAST DAY OF NEXT MONTH'; +------------------------+ | LAST DAY OF NEXT MONTH | +------------------------+ | 2017-11-30             | +------------------------+ 1 row in set (0.00 sec)

Read More

In which format Year(2) or Year(4) MySQL will return the value of year from date '0000-00-00'?

Govinda Sai
Govinda Sai
Updated on 29-Jan-2020 213 Views

Suppose if we have stored a date value as ‘0000-00-00’ in MySQL table then on extracting year value from such kind of date, MySQL will return 0. It would not be in either Year(2) or Year(4) format. To understand it we are using the following data from ‘detail_bday’ table −mysql> Select * from detail_bday; +----+---------+------------+ | Sr | Name    | Birth_Date | +----+---------+------------+ | 1 | Saurabh  | 1990-05-12 | | 2 | Raman    | 1993-06-11 | | 3 | Gaurav   | 1984-01-17 | | 4 | Rahul    | 1993-06-11 | | 5 | Sonia   ...

Read More

What do you mean by default MySQL database for the user?

Manikanth Mani
Manikanth Mani
Updated on 28-Jan-2020 328 Views

Actually, there is no default database for the user. But we have default database for the current session. It can be seen from the following query −mysql> Select Database(); +------------+ | Database() | +------------+ | sample     | +------------+ 1 row in set (0.00 sec)The above result set shows that we are using ‘sample’ database currently. It is set for the current session. We can set another database, with the help of USE statement, also for the current session as follows −mysql> USE query; Database changed mysql> Select Database(); +------------+ | Database() | +------------+ | query     ...

Read More

How can we provide a date with only year (zero months & zero days) value in MySQL?

usharani
usharani
Updated on 28-Jan-2020 281 Views

We can store a date with only year value and have zero months as well as zero-days in MySQL table by disabling NO_ZERO_IN_DATE mode. If this mode is enabled then MySQL will count such kind of date as invalid date and stores all zeros.mysql> Insert into year_testing (OrderDate) values('2017:00:00'); Query OK, 1 row affected (0.09 sec) mysql> select * from year_testing; +------------+ | OrderDate  | +------------+ | 2017-00-00 | +------------+ 1 row in set (0.00 sec) mysql> SET sql_mode = 'NO_ZERO_IN_DATE'; Query OK, 0 rows affected (0.00 sec) mysql> Insert into year_testing(OrderDate) values('2017:00:00'); Query OK, 1 row ...

Read More

How MySQL use YEAR data type to store year value in a table?

varma
varma
Updated on 28-Jan-2020 600 Views

MySQL permits to declare a column YEAR type, with the help of which we can store year values in that column.mysql> Create table year1 (Year_Copyright YEAR); Query OK, 0 rows affected (0.21 sec) mysql> Insert into year1(Year_Copyright) values (2017); Query OK, 1 row affected (0.08 sec) mysql> Select * from year1; +----------------+ | Year_Copyright | +----------------+ |          2017  | +----------------+ 1 row in set (0.00 sec)

Read More

How can we insert current year automatically in a YEAR type column of MySQL table?

Nikitha N
Nikitha N
Updated on 28-Jan-2020 617 Views

It can be done by using either CURDATE() or NOW() in MySQL query as follows −mysql> Insert into year1(Year_Copyright) values (CURDATE()); Query OK, 1 row affected, 1 warning (0.06 sec) mysql> Select * from year1; +----------------+ | Year_Copyright | +----------------+ |           2017 | |           2017 | +----------------+ 2 rows in set (0.00 sec) mysql> Insert into year1(Year_Copyright) values (NOW()); Query OK, 1 row affected, 1 warning (0.06 sec) mysql> Select * from year1; +----------------+ | Year_Copyright | +----------------+ |           2017 | |           2017 | |           2017 | +----------------+ 1 rows in set (0.00 sec)

Read More
Showing 1281–1290 of 3,547 articles
« Prev 1 127 128 129 130 131 355 Next »
Advertisements