

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update column values with date records and set 1 for corresponding records before the current date in SQL
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 OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values(0,'2019-10-11'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values(0,'2018-07-24'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(0,'2018-09-05'); Query OK, 1 row affected (0.27 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------+-------------------+ | ProductStatus | ProductExpiryDate | +---------------+-------------------+ | 0 | 2019-06-12 | | 0 | 2019-10-11 | | 0 | 2018-07-24 | | 0 | 2018-09-05 | +---------------+-------------------+ 4 rows in set (0.00 sec)
Following is the query to set value 1 for records before the current date
mysql> update DemoTable set ProductStatus=1 where ProductExpiryDate <=CURDATE(); Query OK, 3 rows affected (0.95 sec) Rows matched : 3 Changed : 3 Warnings : 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+---------------+-------------------+ | ProductStatus | ProductExpiryDate | +---------------+-------------------+ | 1 | 2019-06-12 | | 0 | 2019-10-11 | | 1 | 2018-07-24 | | 1 | 2018-09-05 | +---------------+-------------------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL queries to update date records with NULL values
- Select a column if condition is met in MySQL to fetch records from current date and current date + 1
- How to get the difference between date records and the current date in MySQL?
- Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
- How to compare the first date and the last date from a MySQL column with date records?
- Condition to check for due date and current date records in MySQL where clause
- How to find last date from records with date values in MySQL?
- Comparison of varchar date records from the current date in MySQL
- Fetch date records comparing with the current date’s day and month in MySQL
- Fetch student records whose result declared 12 days before the current date in MYSQL
- Find the difference between current date and the date records from a MySQL table
- MySQL query to get the current date records wherein one of the columns displays current date
- How to change the date in a table with date records with MySQL?
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- Display all records ignoring the current date record in MySQL
Advertisements