
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Get Maximum Exam Date Using a User-Defined Variable in SQL
To get the maximum exam date with a user-defined variable, the code is as follows −
select date(max(yourColumnName )) into @yourVariableName from yourTableName;
To understand the above syntax, let us first create a table −
mysql> create table DemoTable2001 ( ExamDate date ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2001 values('2019-01-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2001 values('2018-12-31'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2001 values('2018-11-18'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable2001 values('2019-07-25'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2001;
This will produce the following output −
+------------+ | ExamDate | +------------+ | 2019-01-10 | | 2018-12-31 | | 2018-11-18 | | 2019-07-25 | +------------+ 4 rows in set (0.00 sec)
Here is the query to get the maximum date. We will first create a user-defined variable −
mysql> set @comingExamDate:=null; Query OK, 0 rows affected (0.00 sec) mysql> select date(max(ExamDate)) into @comingExamDate from DemoTable2001; Query OK, 1 row affected (0.00 sec) mysql> select @comingExamDate;
This will produce the following output −
+------------------+ | @commingExamDate | +------------------+ | 2019-07-25 | +------------------+ 1 row in set (0.00 sec)
Advertisements